File indexing completed on 2024-05-26 05:14:36

0001 /*
0002     SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QByteArray>
0010 #include <QMimeDatabase>
0011 #include <QMutex>
0012 #include <QObject>
0013 
0014 #include "entities.h"
0015 #include "private/protocol_p.h"
0016 
0017 class QLocalSocket;
0018 
0019 namespace Akonadi
0020 {
0021 namespace Server
0022 {
0023 class NotificationManager;
0024 
0025 class NotificationSubscriber : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit NotificationSubscriber(NotificationManager *manager, quintptr socketDescriptor);
0031     ~NotificationSubscriber() override;
0032 
0033     [[nodiscard]] inline QByteArray subscriber() const
0034     {
0035         return mSubscriber;
0036     }
0037 
0038     [[nodiscard]] QLocalSocket *socket() const
0039     {
0040         return mSocket;
0041     }
0042 
0043     void handleIncomingData();
0044 
0045 public Q_SLOTS:
0046     bool notify(const Akonadi::Protocol::ChangeNotificationPtr &notification);
0047 
0048 private Q_SLOTS:
0049     void socketDisconnected();
0050 
0051 Q_SIGNALS:
0052     void notificationDebuggingChanged(bool enabled);
0053 
0054 protected:
0055     void registerSubscriber(const Protocol::CreateSubscriptionCommand &command);
0056     void modifySubscription(const Protocol::ModifySubscriptionCommand &command);
0057     void disconnectSubscriber();
0058 
0059 private:
0060     bool acceptsNotification(const Protocol::ChangeNotification &notification) const;
0061     bool acceptsItemNotification(const Protocol::ItemChangeNotification &notification) const;
0062     bool acceptsCollectionNotification(const Protocol::CollectionChangeNotification &notification) const;
0063     bool acceptsTagNotification(const Protocol::TagChangeNotification &notification) const;
0064     bool acceptsRelationNotification(const Protocol::RelationChangeNotification &notification) const;
0065     bool acceptsSubscriptionNotification(const Protocol::SubscriptionChangeNotification &notification) const;
0066     bool acceptsDebugChangeNotification(const Protocol::DebugChangeNotification &notification) const;
0067 
0068     bool isCollectionMonitored(Entity::Id id) const;
0069     bool isMimeTypeMonitored(const QString &mimeType) const;
0070     bool isMoveDestinationResourceMonitored(const Protocol::ItemChangeNotification &msg) const;
0071     bool isMoveDestinationResourceMonitored(const Protocol::CollectionChangeNotification &msg) const;
0072 
0073     Protocol::SubscriptionChangeNotificationPtr toChangeNotification() const;
0074 
0075 protected Q_SLOTS:
0076     virtual void writeNotification(const Akonadi::Protocol::ChangeNotificationPtr &notification);
0077 
0078 protected:
0079     explicit NotificationSubscriber(NotificationManager *manager = nullptr);
0080 
0081     void writeCommand(qint64 tag, const Protocol::CommandPtr &cmd);
0082 
0083     mutable QMutex mLock;
0084     NotificationManager *mManager = nullptr;
0085     QLocalSocket *mSocket = nullptr;
0086     QByteArray mSubscriber;
0087     QSet<Entity::Id> mMonitoredCollections;
0088     QSet<Entity::Id> mMonitoredItems;
0089     QSet<Entity::Id> mMonitoredTags;
0090     QSet<Protocol::ModifySubscriptionCommand::ChangeType> mMonitoredTypes;
0091     QSet<QString> mMonitoredMimeTypes;
0092     QSet<QByteArray> mMonitoredResources;
0093     QSet<QByteArray> mIgnoredSessions;
0094     QByteArray mSession;
0095     Protocol::ItemFetchScope mItemFetchScope;
0096     Protocol::CollectionFetchScope mCollectionFetchScope;
0097     Protocol::TagFetchScope mTagFetchScope;
0098     bool mAllMonitored;
0099     bool mExclusive;
0100     bool mNotificationDebugging;
0101 };
0102 
0103 } // namespace Server
0104 } // namespace Akonadi