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