ContactsDisplay.cpp Example File
bbmcontacts/src/ContactsDisplay.cpp
#include "ContactsDisplay.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/platform/bbm/Context>
#include <bb/platform/bbm/ContactService>
#include <bb/platform/bbm/ImageType>
#include <bb/cascades/ListView>
using namespace bb::cascades;
ContactsDisplay::ContactsDisplay(bb::platform::bbm::Context &context, QObject *parent)
: QObject(parent)
, m_context(&context)
, m_contactsDataModel(new QListDataModel<Contact*>())
, m_contactService(0)
{
m_contactsDataModel->setParent(this);
}
void ContactsDisplay::show()
{
QmlDocument *qml = QmlDocument::create("asset:///ContactList.qml").parent(this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);
m_contactService = new bb::platform::bbm::ContactService(m_context, this);
bool ok = QObject::connect(m_contactService, SIGNAL(contactListUpdated()), this, SLOT (updateModel()));
Q_ASSERT(ok);
ok = QObject::connect(m_contactService, SIGNAL(applicationEnabled(const QString&)), this, SLOT (updateModel()));
Q_ASSERT(ok);
ok = QObject::connect(m_contactService, SIGNAL(applicationDisabled(const QString&)), this, SLOT (updateModel()));
Q_ASSERT(ok);
ok = QObject::connect(m_contactService, SIGNAL(contactUpdated(const QString&)), this, SLOT(contactUpdated(const QString&)));
Q_ASSERT(ok);
if (m_contactService->isValid()) {
updateModel();
}
QObject* contactCountField = root->findChild<QObject*>("contactCount");
QString contCount = QString("%1").arg(m_contactService->contactCount());
contactCountField->setProperty("value", contCount);
ListView* contactListView = root->findChild<ListView*>("contactListView");
contactListView->setDataModel(m_contactsDataModel);
qDebug() << contCount;
}
void ContactsDisplay::updateModel() {
QList<bb::platform::bbm::Contact> contacts = m_contactService->contacts();
m_contactsDataModel->clear();
for (int i = 0; i < contacts.size(); i++)
{
Contact* bbm_contact = new Contact(contacts.at(i));
m_contactsDataModel->append(bbm_contact);
bool ok = connect(m_contactService, SIGNAL(displayPictureUpdated(const QString&, const bb::platform::bbm::ImageType::Type
, const QByteArray&))
, bbm_contact
, SLOT(avatarUpdated(const QString&
, const bb::platform::bbm::ImageType::Type
, const QByteArray&)));
Q_ASSERT(ok);
Q_UNUSED(ok);
m_contactService->requestDisplayPicture(bbm_contact->handle());
}
}
void ContactsDisplay::contactUpdated(const QString& handle) {
for(int i = 0; i < m_contactsDataModel->size(); i++) {
QVariantList index;
index << i;
Contact *contact = m_contactsDataModel->value(i);
if(QString::compare(contact->handle(), handle) == 0) {
Contact* new_contact = new Contact(m_contactService->contact(handle));
bool ok = connect(m_contactService, SIGNAL(displayPictureUpdated(const QString&, const bb::platform::bbm::ImageType::Type
, const QByteArray&))
, new_contact
, SLOT(avatarUpdated(const QString&
, const bb::platform::bbm::ImageType::Type
, const QByteArray&)));
Q_ASSERT(ok);
Q_UNUSED(ok);
m_contactService->requestDisplayPicture(new_contact->handle());
m_contactsDataModel->replace(i, new_contact);
}
}
}