1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Copyright (C) 2015 Canonical Ltd
5** Contact: http://www.qt.io/licensing/
6**
7** This file is part of the QtContacts module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL21$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see http://www.qt.io/terms-conditions. For further
16** information use the contact form at http://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 2.1 or version 3 as published by the Free
21** Software Foundation and appearing in the file LICENSE.LGPLv21 and
22** LICENSE.LGPLv3 included in the packaging of this file. Please review the
23** following information to ensure the GNU Lesser General Public License
24** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
25** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26**
27** As a special exception, The Qt Company gives you certain additional
28** rights. These rights are described in The Qt Company LGPL Exception
29** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30**
31** $QT_END_LICENSE$
32**
33****************************************************************************/
34
35#include "qcontactcollectionchangeset.h"
36#include "qcontactcollectionchangeset_p.h"
37
38#include "qcontactmanagerengine.h"
39
40QT_BEGIN_NAMESPACE_CONTACTS
41
42/*!
43 \class QContactCollectionChangeSet
44 \brief The QContactCollectionChangeSet class provides a simple API to simplify the emission
45 of state-change signals for collections from QContactManagerEngine implementations.
46 \inmodule QtContacts
47 \ingroup contacts-main
48
49 This class should only be used by backend developers.
50 */
51
52/*!
53 Constructs a new change set.
54 */
55QContactCollectionChangeSet::QContactCollectionChangeSet()
56 : d(new QContactCollectionChangeSetData)
57{
58}
59
60/*!
61 Constructs a copy of the \a other change set.
62 */
63QContactCollectionChangeSet::QContactCollectionChangeSet(const QContactCollectionChangeSet &other)
64 : d(other.d)
65{
66}
67
68/*!
69 Frees the memory used by this change set.
70 */
71QContactCollectionChangeSet::~QContactCollectionChangeSet()
72{
73}
74
75/*!
76 Assigns this change set to be equal to \a other.
77 */
78QContactCollectionChangeSet &QContactCollectionChangeSet::operator=(const QContactCollectionChangeSet &other)
79{
80 d = other.d;
81 return *this;
82}
83
84/*!
85 Sets the data changed flag to \a dataChanged.
86
87 If this is set to true prior to calling emitSignals(), only the QContactManagerEngine::dataChanged()
88 signal will be emitted; otherwise, the appropriate finer-grained signals will be emitted.
89 */
90void QContactCollectionChangeSet::setDataChanged(bool dataChanged)
91{
92 d->m_dataChanged = dataChanged;
93}
94
95/*!
96 Returns the value of the data changed flag.
97 */
98bool QContactCollectionChangeSet::dataChanged() const
99{
100 return d->m_dataChanged;
101}
102
103/*!
104 Returns the set of IDs of collections which have been added to the database.
105 */
106QSet<QContactCollectionId> QContactCollectionChangeSet::addedCollections() const
107{
108 return d->m_addedCollections;
109}
110
111/*!
112 Inserts the given \a collectionId into the set of IDs of collections which have been added to
113 the database.
114 */
115void QContactCollectionChangeSet::insertAddedCollection(const QContactCollectionId &collectionId)
116{
117 d->m_addedCollections.insert(value: collectionId);
118 d->m_modifiedCollections.append(t: QPair<QContactCollectionId, QContactManager::Operation>(collectionId, QContactManager::Add));
119}
120
121/*!
122 Inserts each of the given \a collectionIds into the set of IDs of collections which have been
123 added to the database.
124 */
125void QContactCollectionChangeSet::insertAddedCollections(const QList<QContactCollectionId> &collectionIds)
126{
127 foreach (const QContactCollectionId &id, collectionIds) {
128 d->m_addedCollections.insert(value: id);
129 d->m_modifiedCollections.append(t: QPair<QContactCollectionId, QContactManager::Operation>(id, QContactManager::Add));
130 }
131}
132
133/*!
134 Clears the set of IDs of collections which have been added to the database.
135 */
136void QContactCollectionChangeSet::clearAddedCollections()
137{
138 d->m_addedCollections.clear();
139}
140
141/*!
142 Returns the set of IDs of collections which have been changed in the database.
143 */
144QSet<QContactCollectionId> QContactCollectionChangeSet::changedCollections() const
145{
146 return d->m_changedCollections;
147}
148
149/*!
150 Inserts the given \a collectionId into the set of IDs of collections which have been changed in
151 the database.
152 */
153void QContactCollectionChangeSet::insertChangedCollection(const QContactCollectionId &collectionId)
154{
155 d->m_changedCollections.insert(value: collectionId);
156 d->m_modifiedCollections.append(t: QPair<QContactCollectionId, QContactManager::Operation>(collectionId, QContactManager::Change));
157}
158
159/*!
160 Inserts each of the given \a collectionIds into the set of IDs of collections which have been
161 changed in the database.
162 */
163void QContactCollectionChangeSet::insertChangedCollections(const QList<QContactCollectionId> &collectionIds)
164{
165 foreach (const QContactCollectionId &id, collectionIds) {
166 d->m_changedCollections.insert(value: id);
167 d->m_modifiedCollections.append(t: QPair<QContactCollectionId, QContactManager::Operation>(id, QContactManager::Change));
168 }
169}
170
171/*!
172 Clears the set of IDs of collections which have been changed in the database.
173 */
174void QContactCollectionChangeSet::clearChangedCollections()
175{
176 d->m_changedCollections.clear();
177}
178
179/*!
180 Returns the set of IDs of collections which have been removed from the database.
181 */
182QSet<QContactCollectionId> QContactCollectionChangeSet::removedCollections() const
183{
184 return d->m_removedCollections;
185}
186
187/*!
188 Inserts the given \a collectionId into the set of IDs of collections which have been removed from
189 the database.
190 */
191void QContactCollectionChangeSet::insertRemovedCollection(const QContactCollectionId &collectionId)
192{
193 d->m_removedCollections.insert(value: collectionId);
194 d->m_modifiedCollections.append(t: QPair<QContactCollectionId, QContactManager::Operation>(collectionId, QContactManager::Remove));
195}
196
197/*!
198 Inserts each of the given \a collectionIds into the set of IDs of collections which have been
199 removed from the database.
200 */
201void QContactCollectionChangeSet::insertRemovedCollections(const QList<QContactCollectionId> &collectionIds)
202{
203 foreach (const QContactCollectionId &id, collectionIds) {
204 d->m_removedCollections.insert(value: id);
205 d->m_modifiedCollections.append(t: QPair<QContactCollectionId, QContactManager::Operation>(id, QContactManager::Remove));
206 }
207}
208
209/*!
210 Clears the set of ids of collections which have been removed from the database.
211 */
212void QContactCollectionChangeSet::clearRemovedCollections()
213{
214 d->m_removedCollections.clear();
215}
216
217/*!
218 Returns the list of ids of contact collections which have been added, changed or removed from
219 the database. The list includes information about which database operation was done. The ids and
220 operations are ordered so that the first operation is first in the list.
221 */
222QList<QPair<QContactCollectionId, QContactManager::Operation> > QContactCollectionChangeSet::modifiedCollections() const
223{
224 return d->m_modifiedCollections;
225}
226
227/*!
228 Clears the list of ids of contact collections which have been added, changed or removed from the database
229 */
230void QContactCollectionChangeSet::clearModifiedCollections()
231{
232 d->m_modifiedCollections.clear();
233}
234
235/*!
236 Clears all flags and sets of IDs in this change set.
237 */
238void QContactCollectionChangeSet::clearAll()
239{
240 d->m_dataChanged = false;
241 d->m_addedCollections.clear();
242 d->m_changedCollections.clear();
243 d->m_removedCollections.clear();
244 d->m_modifiedCollections.clear();
245}
246
247/*!
248 Emits the appropriate signals from the given \a engine given the state of the change set. Note
249 that the flags and sets of IDs are not cleared after signals are emitted.
250 */
251void QContactCollectionChangeSet::emitSignals(QContactManagerEngine *engine) const
252{
253 if (!engine)
254 return;
255
256 if (d->m_dataChanged) {
257 emit engine->dataChanged();
258 } else {
259 if (!d->m_addedCollections.isEmpty())
260 emit engine->collectionsAdded(collectionIds: d->m_addedCollections.toList());
261 if (!d->m_changedCollections.isEmpty())
262 emit engine->collectionsChanged(collectionIds: d->m_changedCollections.toList());
263 if (!d->m_removedCollections.isEmpty())
264 emit engine->collectionsRemoved(collectionIds: d->m_removedCollections.toList());
265 if (!d->m_modifiedCollections.isEmpty())
266 emit engine->collectionsModified(collectionIds: d->m_modifiedCollections);
267
268 }
269}
270
271QT_END_NAMESPACE_CONTACTS
272

source code of qtpim/src/contacts/qcontactcollectionchangeset.cpp