Qt IVI Generator Addressbook Example
#include "addressbook.h"
#include "addressbook_p.h"
#include "addressbookbackendinterface.h"
#include <QQmlEngine>
#include <QIviServiceObject>
#include <QIviProxyServiceObject>
AddressBookPrivate:: AddressBookPrivate(const QString & interface, AddressBook * parent)
: QIviAbstractFeaturePrivate (interface, parent)
, q_ptr(parent)
, m_contacts(nullptr)
{
m_supportsPropertyOverriding = true ;
}
AddressBookPrivate * AddressBookPrivate:: get(AddressBook * v)
{
return reinterpret_cast < AddressBookPrivate * > (v- > d_ptr. data());
}
const AddressBookPrivate * AddressBookPrivate:: get(const AddressBook * v)
{
return reinterpret_cast < const AddressBookPrivate * > (v- > d_ptr. data());
}
AddressBook * AddressBookPrivate:: getParent()
{
return q_ptr;
}
void AddressBookPrivate:: clearToDefaults()
{
if (m_contacts) {
delete m_contacts- > serviceObject();
delete m_contacts;
}
m_contacts = nullptr;
}
void AddressBookPrivate:: onContactsChanged(QIviPagingModelInterface * contacts)
{
QIviPagingModel * old = m_contacts;
if (contacts) {
auto model = new QIviPagingModel ();
model- > setServiceObject(new QIviProxyServiceObject ({ {QIviPagingModel_iid, contacts } }));
m_contacts = model;
auto q = getParent();
emit q- > contactsChanged(model);
} else {
m_contacts = nullptr;
auto q = getParent();
emit q- > contactsChanged(nullptr);
}
if (old) {
delete old- > serviceObject();
delete old;
}
}
bool AddressBookPrivate:: notify(const QByteArray & propertyName, const QVariant & value)
{
auto q = getParent();
if (propertyName = = QByteArray ("contacts" )) {
emit q- > contactsChanged(value. value< QIviPagingModel * > ());
return true ;
}
return QIviAbstractFeaturePrivate :: notify(propertyName, value);
}
AddressBook:: AddressBook(QObject * parent)
: QIviAbstractFeature (* new AddressBookPrivate(QLatin1String(AddressBook_AddressBook_iid), this ), parent)
{
}
AddressBook:: ~ AddressBook()
{
}
void AddressBook:: registerQmlTypes(const QString & uri, int majorVersion, int minorVersion)
{
qmlRegisterType< AddressBook> (uri. toLatin1(), majorVersion, minorVersion, "UiAddressBook" );
}
QIviPagingModel * AddressBook:: contacts() const
{
const auto d = AddressBookPrivate:: get(this );
if (Q_UNLIKELY(d- > m_propertyOverride))
return d- > m_propertyOverride- > property(metaObject()- > indexOfProperty("contacts" )). value< QIviPagingModel * > ();
return d- > m_contacts;
}
QIviPendingReply < void > AddressBook:: insertContact(int index, const Contact & contact)
{
if (AddressBookBackendInterface * backend = addressbookBackend())
return backend- > insertContact(index, contact);
return QIviPendingReply < void > :: createFailedReply();
}
void AddressBook:: connectToServiceObject(QIviServiceObject * serviceObject)
{
auto d = AddressBookPrivate:: get(this );
auto * backend = addressbookBackend();
if (! backend)
return ;
QObjectPrivate :: connect(backend, & AddressBookBackendInterface:: contactsChanged,
d, & AddressBookPrivate:: onContactsChanged);
QIviAbstractFeature :: connectToServiceObject(serviceObject);
}
void AddressBook:: clearServiceObject()
{
auto d = AddressBookPrivate:: get(this );
d- > clearToDefaults();
}
AddressBookBackendInterface * AddressBook:: addressbookBackend() const
{
if (QIviServiceObject * so = serviceObject())
return so- > interfaceInstance< AddressBookBackendInterface* > (interfaceName());
return nullptr;
}
#include "moc_addressbook.cpp"