AddressBook.cpp Example File
addressbook/src/AddressBook.cpp
#include "AddressBook.hpp"
#include "ContactEditor.hpp"
#include "ContactViewer.hpp"
using namespace bb::cascades;
using namespace bb::pim::contacts;
AddressBook::AddressBook(QObject *parent)
: QObject(parent)
, m_contactService(new ContactService(this))
, m_model(new GroupDataModel(this))
, m_contactViewer(new ContactViewer(m_contactService, this))
, m_contactEditor(new ContactEditor(m_contactService, this))
, m_currentContactId(-1)
{
m_model->setGrouping(ItemGrouping::None);
bool ok = connect(m_contactService, SIGNAL(contactsAdded(QList<int>)), SLOT(filterContacts()));
Q_ASSERT(ok);
ok = connect(m_contactService, SIGNAL(contactsChanged(QList<int>)), SLOT(filterContacts()));
Q_ASSERT(ok);
ok = connect(m_contactService, SIGNAL(contactsDeleted(QList<int>)), SLOT(filterContacts()));
Q_ASSERT(ok);
filterContacts();
}
void AddressBook::setCurrentContact(const QVariantList &indexPath)
{
if (indexPath.isEmpty()) {
m_currentContactId = -1;
} else {
const QVariantMap entry = m_model->data(indexPath).toMap();
m_currentContactId = entry.value("contactId").toInt();
}
}
void AddressBook::createContact()
{
m_contactEditor->reset();
m_contactEditor->setMode(ContactEditor::CreateMode);
}
void AddressBook::editContact()
{
m_contactEditor->loadContact(m_currentContactId);
m_contactEditor->setMode(ContactEditor::EditMode);
}
void AddressBook::viewContact()
{
m_contactViewer->setContactId(m_currentContactId);
}
void AddressBook::deleteContact()
{
m_contactService->deleteContact(m_currentContactId);
}
bb::cascades::GroupDataModel* AddressBook::model() const
{
return m_model;
}
QString AddressBook::filter() const
{
return m_filter;
}
void AddressBook::setFilter(const QString &filter)
{
if (m_filter == filter)
return;
m_filter = filter;
emit filterChanged();
filterContacts();
}
ContactViewer* AddressBook::contactViewer() const
{
return m_contactViewer;
}
ContactEditor* AddressBook::contactEditor() const
{
return m_contactEditor;
}
void AddressBook::filterContacts()
{
QList<Contact> contacts;
if (m_filter.isEmpty()) {
ContactListFilters filter;
contacts = m_contactService->contacts(filter);
} else {
ContactSearchFilters filter;
filter.setSearchValue(m_filter);
contacts = m_contactService->searchContacts(filter);
}
m_model->clear();
foreach (const Contact &idContact, contacts) {
const Contact contact = m_contactService->contactDetails(idContact.id());
QVariantMap entry;
entry["contactId"] = contact.id();
entry["firstName"] = contact.firstName();
entry["lastName"] = contact.lastName();
const QList<ContactAttribute> emails = contact.emails();
if (!emails.isEmpty())
entry["email"] = emails.first().value();
m_model->insert(entry);
}
}