1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtQml module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
41
#include <QtQml/qqmlinfo.h>
42
#include "qdeclarativecontactrelationshipmodel_p.h"
43
#include "qcontactmanager.h"
44
#include "qcontactrequests.h"
48
QT_BEGIN_NAMESPACE_CONTACTS
51
\qmltype RelationshipModel
52
\instantiates QDeclarativeContactRelationshipModel
53
\brief The RelationshipModel provides a model of contact relationships from the contacts store.
55
\ingroup qml-contacts-main
56
\inqmlmodule QtContacts 5.0
58
This element is part of the \b{QtContacts} module.
60
The contents of the model can be specified with \l participantId, \l role and \l relationshipType properties.
61
Whether the model is automatically updated when the store or filter changes, can be controlled
62
with \l RelationshipModel::autoUpdate property.
64
There are two ways of accessing the relationship data: through model by using views and delegates,
65
or alternatively via \l relationships list property.
67
At the moment only data role provided by the model is \c relationship (\l Relationship).
68
Through that one can access any data provided by the Relationship element.
70
\sa Relationship, {QContactRelationship}
73
class QDeclarativeContactRelationshipModelPrivate
76
QDeclarativeContactRelationshipModelPrivate()
79
, m_role(QDeclarativeContactRelationship::Either)
83
~QDeclarativeContactRelationshipModelPrivate()
88
QContactManager *m_manager;
89
QDeclarativeContactRelationship m_relationshipTypeHolder;
90
QDeclarativeContact* m_participant;
91
QDeclarativeContactRelationship::RelationshipRole m_role;
92
QList<QContactRelationship> m_relationships;
93
QList<QDeclarativeContactRelationship *> m_declarativeRelationships;
96
QDeclarativeContactRelationshipModel::QDeclarativeContactRelationshipModel(QObject *parent)
97
: QAbstractListModel(parent)
98
, d(new QDeclarativeContactRelationshipModelPrivate)
100
QHash<int, QByteArray> roleNames;
101
roleNames = QAbstractItemModel::roleNames();
102
roleNames.insert(RelationshipRole, "relationship");
103
setRoleNames(roleNames);
104
connect(this, SIGNAL(managerChanged()), SLOT(fetchAgain()));
105
connect(this, SIGNAL(participantChanged()), SLOT(fetchAgain()));
106
connect(this, SIGNAL(relationshipTypeChanged()), SLOT(fetchAgain()));
107
connect(this, SIGNAL(roleChanged()), SLOT(fetchAgain()));
110
QDeclarativeContactRelationshipModel::~QDeclarativeContactRelationshipModel()
116
\qmlproperty string RelationshipModel::manager
118
This property holds the manager uri of the contact backend engine.
120
QString QDeclarativeContactRelationshipModel::manager() const
123
return d->m_manager->managerName();
129
\qmlproperty string RelationshipModel::error
131
This property holds the latest error code returned by the contact manager.
133
This property is read only.
135
QString QDeclarativeContactRelationshipModel::error() const
137
switch (d->m_manager->error()) {
138
case QContactManager::DoesNotExistError:
139
return QStringLiteral("DoesNotExist");
140
case QContactManager::AlreadyExistsError:
141
return QStringLiteral("AlreadyExists");
142
case QContactManager::InvalidDetailError:
143
return QStringLiteral("InvalidDetail");
144
case QContactManager::InvalidRelationshipError:
145
return QStringLiteral("InvalidRelationship");
146
case QContactManager::LockedError:
147
return QStringLiteral("LockedError");
148
case QContactManager::DetailAccessError:
149
return QStringLiteral("DetailAccessError");
150
case QContactManager::PermissionsError:
151
return QStringLiteral("PermissionsError");
152
case QContactManager::OutOfMemoryError:
153
return QStringLiteral("OutOfMemory");
154
case QContactManager::NotSupportedError:
155
return QStringLiteral("NotSupported");
156
case QContactManager::BadArgumentError:
157
return QStringLiteral("BadArgument");
158
case QContactManager::UnspecifiedError:
159
return QStringLiteral("UnspecifiedError");
160
case QContactManager::VersionMismatchError:
161
return QStringLiteral("VersionMismatch");
162
case QContactManager::LimitReachedError:
163
return QStringLiteral("LimitReached");
164
case QContactManager::InvalidContactTypeError:
165
return QStringLiteral("InvalidContactType");
169
return QStringLiteral("NoError");
171
void QDeclarativeContactRelationshipModel::setManager(const QString& manager)
173
if (d->m_manager == 0 || manager != d->m_manager->managerName() ) {
174
d->m_manager = new QContactManager(manager,QMap<QString,QString>(), this);
175
connect(d->m_manager,SIGNAL(relationshipsAdded(const QList<QContactId>&)), this, SLOT(fetchAgain()));
176
connect(d->m_manager,SIGNAL(relationshipsRemoved(const QList<QContactId>&)), this, SLOT(fetchAgain()));
177
emit managerChanged();
183
\qmlproperty int RelationshipModel::participantId
185
This property holds the participant which the list of relationships returned by RelationshipModel should contain.
187
\sa RelationshipFilter::relatedContactId
188
\sa RelationshipModel::role
190
QDeclarativeContact* QDeclarativeContactRelationshipModel::participant() const
192
return d->m_participant;
194
void QDeclarativeContactRelationshipModel::setParticipant(QDeclarativeContact* participant)
196
if (d->m_participant != participant) {
197
d->m_participant = participant;
198
emit participantChanged();
203
\qmlproperty variant RelationshipModel::relationshipType
205
This property holds the relationship type which the list of relationships returned by RelationshipModel should contain.
207
\sa Relationship::type
209
QVariant QDeclarativeContactRelationshipModel::relationshipType() const
211
return d->m_relationshipTypeHolder.relationshipType();
213
void QDeclarativeContactRelationshipModel::setRelationshipType(const QVariant& type)
215
if (type != relationshipType()) {
216
d->m_relationshipTypeHolder.setRelationshipType(type);
217
emit relationshipTypeChanged();
222
\qmlproperty enumeration RelationshipModel::role
224
This property holds the relationship role which the list of relationships returned by RelationshipModel should contain.
226
\sa RelationshipFilter::relatedContactRole
228
QDeclarativeContactRelationship::RelationshipRole QDeclarativeContactRelationshipModel::role() const
232
void QDeclarativeContactRelationshipModel::setRole(QDeclarativeContactRelationship::RelationshipRole role)
234
if (d->m_role != role) {
241
\qmlproperty bool RelationshipModel::autoUpdate
243
This property indicates whether or not the relationship model should be updated automatically, default value is true.
245
bool QDeclarativeContactRelationshipModel::autoUpdate() const
250
void QDeclarativeContactRelationshipModel::setAutoUpdate(bool autoUpdate)
252
Q_UNUSED(autoUpdate);
257
\qmlproperty list<Relationship> RelationshipModel::relationships
259
This property holds a list of relationships.
263
QQmlListProperty<QDeclarativeContactRelationship> QDeclarativeContactRelationshipModel::relationships()
265
return QQmlListProperty<QDeclarativeContactRelationship>(this, d->m_declarativeRelationships);
268
int QDeclarativeContactRelationshipModel::rowCount(const QModelIndex &parent) const
271
return d->m_declarativeRelationships.count();
274
QVariant QDeclarativeContactRelationshipModel::data(const QModelIndex &index, int role) const
276
QDeclarativeContactRelationship* dcr = d->m_declarativeRelationships.value(index.row());
277
if (role == RelationshipRole) {
278
return QVariant::fromValue(dcr);
279
} else if (role == Qt::DisplayRole) {
280
return QString(QStringLiteral("%1 %2 %3")).arg(dcr->relationship().first().id().toString()).arg(dcr->relationship().relationshipType()).arg(dcr->relationship().second().id().toString());
285
void QDeclarativeContactRelationshipModel::fetchAgain()
288
QContactRelationshipFetchRequest* req = new QContactRelationshipFetchRequest(this);
289
req->setManager(d->m_manager);
290
if (d->m_participant) {
291
QContact contact (d->m_participant->contact());
292
if (d->m_role == QDeclarativeContactRelationship::First || d->m_role == QDeclarativeContactRelationship::Either)
293
req->setFirst(contact);
294
if (d->m_role == QDeclarativeContactRelationship::Second || d->m_role == QDeclarativeContactRelationship::Either)
295
req->setSecond(contact);
296
req->setRelationshipType(d->m_relationshipTypeHolder.relationship().relationshipType());
297
connect(req,SIGNAL(stateChanged(QContactAbstractRequest::State)), this, SLOT(requestUpdated()));
304
\qmlmethod RelationshipModel::addRelationship(relationship)
305
Addes the given \a relationship to the backend store.
307
void QDeclarativeContactRelationshipModel::addRelationship(QDeclarativeContactRelationship* dcr)
310
QContactRelationship cr = dcr->relationship();
311
QContactRelationshipSaveRequest* req = new QContactRelationshipSaveRequest(this);
312
req->setManager(d->m_manager);
313
req->setRelationship(cr);
314
connect(req, SIGNAL(stateChanged(QContactAbstractRequest::State)), this, SLOT(relationshipsSaved()));
320
\qmlmethod RelationshipModel::removeRelationship(relationship)
321
Removes the given \a relationship from the backend store.
323
void QDeclarativeContactRelationshipModel::removeRelationship(QDeclarativeContactRelationship* dcr)
326
QContactRelationship cr = dcr->relationship();
327
QContactRelationshipRemoveRequest* req = new QContactRelationshipRemoveRequest(this);
328
req->setManager(d->m_manager);
329
req->setRelationship(cr);
330
connect(req,SIGNAL(stateChanged(QContactAbstractRequest::State)), this, SLOT(relationshipsRemoved()));
335
void QDeclarativeContactRelationshipModel::requestUpdated()
337
QContactRelationshipFetchRequest* req = qobject_cast<QContactRelationshipFetchRequest*>(sender());
338
if (req->isFinished() && req->error() == QContactManager::NoError) {
340
QList<QContactRelationship> relationships = req->relationships();
343
beginInsertRows(QModelIndex(), 0, relationships.count());
345
foreach(QDeclarativeContactRelationship* dcr, d->m_declarativeRelationships) {
348
d->m_declarativeRelationships.clear();
349
d->m_relationships.clear();
351
foreach (const QContactRelationship& cr, relationships) {
352
QDeclarativeContactRelationship* dcr = new QDeclarativeContactRelationship(this);
353
dcr->setRelationship(cr);
354
d->m_declarativeRelationships.append(dcr);
355
d->m_relationships.append(cr);
359
emit relationshipsChanged();
363
void QDeclarativeContactRelationshipModel::relationshipsSaved()
365
QContactRelationshipSaveRequest* req = qobject_cast<QContactRelationshipSaveRequest*>(sender());
367
if (req->isFinished()) {
368
QList<QContactRelationship> rs = req->relationships();
369
QList<int> errorIds = req->errorMap().keys();
371
for( int i = 0; i < rs.count(); i++) {
372
if (!errorIds.contains(i)) {
374
QContactRelationship r = rs.at(i);
376
if (!d->m_relationships.contains(r)) {
377
//new relationship saved
378
QDeclarativeContactRelationship* dcr = new QDeclarativeContactRelationship(this);
379
dcr->setRelationship(r);
380
beginInsertRows(QModelIndex(), d->m_declarativeRelationships.count(), d->m_declarativeRelationships.count());
381
d->m_declarativeRelationships.append(dcr);
382
d->m_relationships.append(r);
391
void QDeclarativeContactRelationshipModel::relationshipsRemoved()
393
QContactRelationshipRemoveRequest* req = qobject_cast<QContactRelationshipRemoveRequest*>(sender());
395
if (req->isFinished()) {
396
QList<QContactRelationship> rs = req->relationships();
397
QList<int> errorIds = req->errorMap().keys();
400
for( int i = 0; i < rs.count(); i++) {
401
if (!errorIds.contains(i)) {
403
QContactRelationship r = rs.at(i);
404
for (; row < d->m_relationships.count(); row++) {
405
if (d->m_relationships.at(row) == r)
408
if (row < d->m_relationships.count()) {
409
beginRemoveRows(QModelIndex(), row, row);
410
d->m_declarativeRelationships.removeAt(row);
411
d->m_relationships.removeAt(row);
415
qmlInfo(this) << tr("this relationship '") << row << tr("' was already removed!");
423
#include "moc_qdeclarativecontactrelationshipmodel_p.cpp"
425
QT_END_NAMESPACE_CONTACTS