File indexing completed on 2024-06-23 05:07:09

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "entities.h"
0010 
0011 #include "private/protocol_p.h"
0012 
0013 #include <QByteArray>
0014 #include <QList>
0015 #include <QString>
0016 
0017 namespace Akonadi
0018 {
0019 namespace Server
0020 {
0021 class DataStore;
0022 class Connection;
0023 class AkonadiServer;
0024 
0025 /**
0026   Part of the DataStore, collects change notifications and emits
0027   them after the current transaction has been successfully committed.
0028   Where possible, notifications are compressed.
0029 */
0030 class NotificationCollector
0031 {
0032 public:
0033     /**
0034       Create a new notification collector for the given DataStore @p db.
0035       @param db The datastore using this notification collector.
0036     */
0037     explicit NotificationCollector(AkonadiServer &akonadi, DataStore *db);
0038 
0039     /**
0040       Destroys this notification collector.
0041     */
0042     virtual ~NotificationCollector() = default;
0043 
0044     /**
0045      * Sets the connection that is causing the changes.
0046      */
0047     void setConnection(Connection *connection);
0048 
0049     /**
0050       Notify about an added item.
0051       Provide as many parameters as you have at hand currently, everything
0052       that is missing will be looked up in the database later.
0053     */
0054     void itemAdded(const PimItem &item, bool seen, const Collection &collection = Collection(), const QByteArray &resource = QByteArray());
0055 
0056     /**
0057       Notify about a changed item.
0058       Provide as many parameters as you have at hand currently, everything
0059       that is missing will be looked up in the database later.
0060     */
0061     void itemChanged(const PimItem &item,
0062                      const QSet<QByteArray> &changedParts,
0063                      const Collection &collection = Collection(),
0064                      const QByteArray &resource = QByteArray());
0065 
0066     /**
0067       Notify about changed items flags
0068       Provide as many parameters as you have at hand currently, everything
0069       that is missing will be looked up in the database later.
0070     */
0071     void itemsFlagsChanged(const PimItem::List &items,
0072                            const QSet<QByteArray> &addedFlags,
0073                            const QSet<QByteArray> &removedFlags,
0074                            const Collection &collection = Collection(),
0075                            const QByteArray &resource = QByteArray());
0076 
0077     /**
0078      Notify about changed items tags
0079     **/
0080     void itemsTagsChanged(const PimItem::List &items,
0081                           const QSet<qint64> &addedTags,
0082                           const QSet<qint64> &removedTags,
0083                           const Collection &collection = Collection(),
0084                           const QByteArray &resource = QByteArray());
0085 
0086     /**
0087      Notify about changed items relations
0088     **/
0089     void itemsRelationsChanged(const PimItem::List &items,
0090                                const Relation::List &addedRelations,
0091                                const Relation::List &removedRelations,
0092                                const Collection &collection = Collection(),
0093                                const QByteArray &resource = QByteArray());
0094 
0095     /**
0096       Notify about moved items
0097       Provide as many parameters as you have at hand currently, everything
0098       that is missing will be looked up in the database later.
0099     */
0100     void itemsMoved(const PimItem::List &items,
0101                     const Collection &collectionSrc = Collection(),
0102                     const Collection &collectionDest = Collection(),
0103                     const QByteArray &sourceResource = QByteArray());
0104 
0105     /**
0106       Notify about removed items.
0107       Make sure you either provide all parameters or call this function before
0108       actually removing the item from database.
0109     */
0110     void itemsRemoved(const PimItem::List &items, const Collection &collection = Collection(), const QByteArray &resource = QByteArray());
0111 
0112     /**
0113      * Notify about linked items
0114      */
0115     void itemsLinked(const PimItem::List &items, const Collection &collection);
0116 
0117     /**
0118      * Notify about unlinked items.
0119      */
0120     void itemsUnlinked(const PimItem::List &items, const Collection &collection);
0121 
0122     /**
0123       Notify about a added collection.
0124       Provide as many parameters as you have at hand currently, everything
0125       that is missing will be looked up in the database later.
0126      */
0127     void collectionAdded(const Collection &collection, const QByteArray &resource = QByteArray());
0128 
0129     /**
0130       Notify about a changed collection.
0131       Provide as many parameters as you have at hand currently, everything
0132       that is missing will be looked up in the database later.
0133     */
0134     void collectionChanged(const Collection &collection, const QList<QByteArray> &changes, const QByteArray &resource = QByteArray());
0135 
0136     /**
0137       Notify about a moved collection.
0138       Provide as many parameters as you have at hand currently, everything
0139       missing will be looked up on demand in the database later.
0140     */
0141     void collectionMoved(const Collection &collection,
0142                          const Collection &source,
0143                          const QByteArray &resource = QByteArray(),
0144                          const QByteArray &destResource = QByteArray());
0145 
0146     /**
0147       Notify about a removed collection.
0148       Make sure you either provide all parameters or call this function before
0149       actually removing the item from database.
0150      */
0151     void collectionRemoved(const Collection &collection, const QByteArray &resource = QByteArray());
0152 
0153     /**
0154      *      Notify about a collection subscription.
0155      */
0156     void collectionSubscribed(const Collection &collection, const QByteArray &resource = QByteArray());
0157     /**
0158      *      Notify about a collection unsubscription
0159      */
0160     void collectionUnsubscribed(const Collection &collection, const QByteArray &resource = QByteArray());
0161 
0162     /**
0163       Notify about an added tag.
0164      */
0165     void tagAdded(const Tag &tag);
0166 
0167     /**
0168      Notify about a changed tag.
0169      */
0170     void tagChanged(const Tag &tag);
0171 
0172     /**
0173       Notify about a removed tag.
0174      */
0175     void tagRemoved(const Tag &tag, const QByteArray &resource, const QString &remoteId);
0176 
0177     /**
0178       Notify about an added relation.
0179      */
0180     void relationAdded(const Relation &relation);
0181 
0182     /**
0183       Notify about a removed relation.
0184      */
0185     void relationRemoved(const Relation &relation);
0186 
0187     /**
0188       Trigger sending of collected notifications.
0189 
0190       @returns Returns true when any notifications were dispatched, false if there
0191                were no pending notifications.
0192     */
0193     bool dispatchNotifications();
0194 
0195 private:
0196     void itemNotification(Protocol::ItemChangeNotification::Operation op,
0197                           const PimItem::List &items,
0198                           const Collection &collection,
0199                           const Collection &collectionDest,
0200                           const QByteArray &resource,
0201                           const QSet<QByteArray> &parts = QSet<QByteArray>(),
0202                           const QSet<QByteArray> &addedFlags = QSet<QByteArray>(),
0203                           const QSet<QByteArray> &removedFlags = QSet<QByteArray>(),
0204                           const QSet<qint64> &addedTags = QSet<qint64>(),
0205                           const QSet<qint64> &removedTags = QSet<qint64>(),
0206                           const Relation::List &addedRelations = Relation::List(),
0207                           const Relation::List &removedRelations = Relation::List());
0208     void itemNotification(Protocol::ItemChangeNotification::Operation op,
0209                           const PimItem &item,
0210                           const Collection &collection,
0211                           const Collection &collectionDest,
0212                           const QByteArray &resource,
0213                           const QSet<QByteArray> &parts = QSet<QByteArray>());
0214     void collectionNotification(Protocol::CollectionChangeNotification::Operation op,
0215                                 const Collection &collection,
0216                                 Collection::Id source,
0217                                 Collection::Id destination,
0218                                 const QByteArray &resource,
0219                                 const QSet<QByteArray> &changes = QSet<QByteArray>(),
0220                                 const QByteArray &destResource = QByteArray());
0221     void tagNotification(Protocol::TagChangeNotification::Operation op,
0222                          const Tag &tag,
0223                          const QByteArray &resource = QByteArray(),
0224                          const QString &remoteId = QString());
0225     void relationNotification(Protocol::RelationChangeNotification::Operation op, const Relation &relation);
0226     void dispatchNotification(const Protocol::ChangeNotificationPtr &msg);
0227     void clear();
0228 
0229     void completeNotification(const Protocol::ChangeNotificationPtr &msg);
0230 
0231 protected:
0232     virtual void notify(Protocol::ChangeNotificationList &&ntfs);
0233 
0234 private:
0235     Q_DISABLE_COPY_MOVE(NotificationCollector)
0236 
0237     DataStore *mDb;
0238     Connection *mConnection = nullptr;
0239     AkonadiServer &mAkonadi;
0240     bool mIgnoreTransactions = false;
0241 
0242     Protocol::ChangeNotificationList mNotifications;
0243 };
0244 
0245 } // namespace Server
0246 } // namespace Akonadi