1 /*
2     SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QByteArray>
10 #include <QMimeDatabase>
11 #include <QMutex>
12 #include <QObject>
13 
14 #include "entities.h"
15 #include <private/protocol_p.h>
16 
17 class QLocalSocket;
18 
19 namespace Akonadi
20 {
21 namespace Server
22 {
23 class NotificationManager;
24 
25 class NotificationSubscriber : public QObject
26 {
27     Q_OBJECT
28 
29 public:
30     explicit NotificationSubscriber(NotificationManager *manager, quintptr socketDescriptor);
31     ~NotificationSubscriber() override;
32 
subscriber()33     Q_REQUIRED_RESULT inline QByteArray subscriber() const
34     {
35         return mSubscriber;
36     }
37 
socket()38     Q_REQUIRED_RESULT QLocalSocket *socket() const
39     {
40         return mSocket;
41     }
42 
43     void handleIncomingData();
44 
45 public Q_SLOTS:
46     bool notify(const Akonadi::Protocol::ChangeNotificationPtr &notification);
47 
48 private Q_SLOTS:
49     void socketDisconnected();
50 
51 Q_SIGNALS:
52     void notificationDebuggingChanged(bool enabled);
53 
54 protected:
55     void registerSubscriber(const Protocol::CreateSubscriptionCommand &command);
56     void modifySubscription(const Protocol::ModifySubscriptionCommand &command);
57     void disconnectSubscriber();
58 
59 private:
60     bool acceptsNotification(const Protocol::ChangeNotification &notification) const;
61     bool acceptsItemNotification(const Protocol::ItemChangeNotification &notification) const;
62     bool acceptsCollectionNotification(const Protocol::CollectionChangeNotification &notification) const;
63     bool acceptsTagNotification(const Protocol::TagChangeNotification &notification) const;
64     bool acceptsRelationNotification(const Protocol::RelationChangeNotification &notification) const;
65     bool acceptsSubscriptionNotification(const Protocol::SubscriptionChangeNotification &notification) const;
66     bool acceptsDebugChangeNotification(const Protocol::DebugChangeNotification &notification) const;
67 
68     bool isCollectionMonitored(Entity::Id id) const;
69     bool isMimeTypeMonitored(const QString &mimeType) const;
70     bool isMoveDestinationResourceMonitored(const Protocol::ItemChangeNotification &msg) const;
71     bool isMoveDestinationResourceMonitored(const Protocol::CollectionChangeNotification &msg) const;
72 
73     Protocol::SubscriptionChangeNotificationPtr toChangeNotification() const;
74 
75 protected Q_SLOTS:
76     virtual void writeNotification(const Akonadi::Protocol::ChangeNotificationPtr &notification);
77 
78 protected:
79     explicit NotificationSubscriber(NotificationManager *manager = nullptr);
80 
81     void writeCommand(qint64 tag, const Protocol::CommandPtr &cmd);
82 
83     mutable QMutex mLock;
84     NotificationManager *mManager = nullptr;
85     QLocalSocket *mSocket = nullptr;
86     QByteArray mSubscriber;
87     QSet<Entity::Id> mMonitoredCollections;
88     QSet<Entity::Id> mMonitoredItems;
89     QSet<Entity::Id> mMonitoredTags;
90     QSet<Protocol::ModifySubscriptionCommand::ChangeType> mMonitoredTypes;
91     QSet<QString> mMonitoredMimeTypes;
92     QSet<QByteArray> mMonitoredResources;
93     QSet<QByteArray> mIgnoredSessions;
94     QByteArray mSession;
95     Protocol::ItemFetchScope mItemFetchScope;
96     Protocol::CollectionFetchScope mCollectionFetchScope;
97     Protocol::TagFetchScope mTagFetchScope;
98     bool mAllMonitored;
99     bool mExclusive;
100     bool mNotificationDebugging;
101 };
102 
103 } // namespace Server
104 } // namespace Akonadi
105 
106