ContactEditor.cpp Example File
addressbook/src/ContactEditor.cpp
#include "ContactEditor.hpp"
#include <bb/pim/contacts/Contact>
#include <bb/pim/contacts/ContactAttributeBuilder>
#include <bb/pim/contacts/ContactBuilder>
using namespace bb::pim::contacts;
template <typename T>
static void updateContactAttribute(ContactBuilder &builder, const Contact &contact,
AttributeKind::Type kind, AttributeSubKind::Type subKind,
const T &value)
{
QList<ContactAttribute> attributes = contact.filteredAttributes(kind);
foreach (const ContactAttribute &attribute, attributes) {
if (attribute.subKind() == subKind)
builder.deleteAttribute(attribute);
}
builder.addAttribute(ContactAttributeBuilder()
.setKind(kind)
.setSubKind(subKind)
.setValue(value));
}
ContactEditor::ContactEditor(ContactService *service, QObject *parent)
: QObject(parent)
, m_contactService(service)
, m_contactId(-1)
, m_birthday(QDateTime::currentDateTime())
, m_mode(CreateMode)
{
}
void ContactEditor::loadContact(ContactId contactId)
{
m_contactId = contactId;
const Contact contact = m_contactService->contactDetails(m_contactId);
m_firstName = contact.firstName();
m_lastName = contact.lastName();
m_birthday = QDateTime::currentDateTime();
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();
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
}
void ContactEditor::saveContact()
{
if (m_mode == CreateMode) {
ContactBuilder builder;
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameGiven)
.setValue(m_firstName));
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameSurname)
.setValue(m_lastName));
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Date)
.setSubKind(AttributeSubKind::DateBirthday)
.setValue(m_birthday));
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Email)
.setSubKind(AttributeSubKind::Other)
.setValue(m_email));
m_contactService->createContact(builder, false);
} else if (m_mode == EditMode) {
Contact contact = m_contactService->contactDetails(m_contactId);
if (contact.id()) {
ContactBuilder builder = contact.edit();
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameGiven, m_firstName);
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameSurname, m_lastName);
updateContactAttribute<QDateTime>(builder, contact, AttributeKind::Date, AttributeSubKind::DateBirthday, m_birthday);
updateContactAttribute<QString>(builder, contact, AttributeKind::Email, AttributeSubKind::Other, m_email);
m_contactService->updateContact(builder);
}
}
}
void ContactEditor::reset()
{
m_firstName.clear();
m_lastName.clear();
m_birthday = QDateTime::currentDateTime();
m_email.clear();
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
}
void ContactEditor::setFirstName(const QString &firstName)
{
if (m_firstName == firstName)
return;
m_firstName = firstName;
emit firstNameChanged();
}
QString ContactEditor::firstName() const
{
return m_firstName;
}
void ContactEditor::setLastName(const QString &lastName)
{
if (m_lastName == lastName)
return;
m_lastName = lastName;
emit lastNameChanged();
}
QString ContactEditor::lastName() const
{
return m_lastName;
}
void ContactEditor::setBirthday(const QDateTime &birthday)
{
if (m_birthday.date() == birthday.date())
return;
m_birthday = birthday;
emit birthdayChanged();
}
QDateTime ContactEditor::birthday() const
{
return m_birthday;
}
void ContactEditor::setEmail(const QString &email)
{
if (m_email == email)
return;
m_email = email;
emit emailChanged();
}
QString ContactEditor::email() const
{
return m_email;
}
void ContactEditor::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
ContactEditor::Mode ContactEditor::mode() const
{
return m_mode;
}