ContactViewer.cpp Example File
addressbook/src/ContactViewer.cpp
#include "ContactViewer.hpp"
#include <bb/pim/contacts/Contact>
using namespace bb::pim::contacts;
ContactViewer::ContactViewer(ContactService *service, QObject *parent)
: QObject(parent)
, m_contactService(service)
, m_contactId(-1)
{
bool ok = connect(m_contactService, SIGNAL(contactsChanged(QList<int>)), SLOT(contactsChanged(QList<int>)));
Q_ASSERT(ok);
Q_UNUSED(ok);
}
void ContactViewer::updateContact()
{
const QString oldFirstName = m_firstName;
const QString oldLastName = m_lastName;
const QDateTime oldBirthday = m_birthday;
const QString oldEmail = m_email;
const Contact contact = m_contactService->contactDetails(m_contactId);
m_firstName = contact.firstName();
m_lastName = contact.lastName();
m_birthday = QDateTime();
const QList<ContactAttribute> dateAttributes = contact.filteredAttributes(AttributeKind::Date);
foreach (const ContactAttribute &dateAttribute, dateAttributes) {
if (dateAttribute.subKind() == AttributeSubKind::DateBirthday)
m_birthday = dateAttribute.valueAsDateTime();
}
m_email.clear();
const QList<ContactAttribute> emails = contact.emails();
if (!emails.isEmpty())
m_email = emails.first().value();
if (oldFirstName != m_firstName)
emit firstNameChanged();
if (oldLastName != m_lastName)
emit lastNameChanged();
if (oldBirthday != m_birthday)
emit birthdayChanged();
if (oldEmail != m_email)
emit emailChanged();
}
void ContactViewer::contactsChanged(const QList<int> &contactIds)
{
if (contactIds.contains(m_contactId))
updateContact();
}
void ContactViewer::setContactId(ContactId contactId)
{
if (m_contactId == contactId)
return;
m_contactId = contactId;
updateContact();
}
QString ContactViewer::firstName() const
{
return m_firstName;
}
QString ContactViewer::lastName() const
{
return m_lastName;
}
QDateTime ContactViewer::birthday() const
{
return m_birthday;
}
QString ContactViewer::formattedBirthday() const
{
if (!m_birthday.isValid())
return tr("n/a");
else
return m_birthday.date().toString();
}
QString ContactViewer::email() const
{
return m_email;
}