File indexing completed on 2024-11-10 04:40:11
0001 /* 0002 SPDX-FileCopyrightText: 2011 Stephen Kelly <steveire@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include "akonaditestfake_export.h" 0010 #include "collectionfetchscope.h" 0011 #include "itemfetchscope.h" 0012 #include "monitor_p.h" 0013 #include "notificationsource_p.h" 0014 #include "private/protocol_p.h" 0015 0016 template<typename T, typename Cache> 0017 class FakeEntityCache : public Cache 0018 { 0019 public: 0020 FakeEntityCache(Akonadi::Session *session = nullptr, QObject *parent = nullptr) 0021 : Cache(0, session, parent) 0022 { 0023 } 0024 0025 void setData(const QHash<typename T::Id, T> &data) 0026 { 0027 m_data = data; 0028 } 0029 0030 void insert(T t) 0031 { 0032 m_data.insert(t.id(), t); 0033 } 0034 0035 void emitDataAvailable() 0036 { 0037 Q_EMIT Cache::dataAvailable(); 0038 } 0039 0040 T retrieve(typename T::Id id) const override 0041 { 0042 return m_data.value(id); 0043 } 0044 0045 void request(typename T::Id id, const typename Cache::FetchScope &scope) override 0046 { 0047 Q_UNUSED(id) 0048 Q_UNUSED(scope) 0049 } 0050 0051 bool ensureCached(typename T::Id id, const typename Cache::FetchScope &scope) override 0052 { 0053 Q_UNUSED(scope) 0054 return m_data.contains(id); 0055 } 0056 0057 private: 0058 QHash<typename T::Id, T> m_data; 0059 }; 0060 using FakeCollectionCache = FakeEntityCache<Akonadi::Collection, Akonadi::CollectionCache>; 0061 using FakeItemCache = FakeEntityCache<Akonadi::Item, Akonadi::ItemCache>; 0062 0063 class AKONADITESTFAKE_EXPORT FakeNotificationSource : public QObject 0064 { 0065 Q_OBJECT 0066 public: 0067 explicit FakeNotificationSource(QObject *parent = nullptr) 0068 : QObject(parent) 0069 { 0070 } 0071 0072 public Q_SLOTS: 0073 void setAllMonitored(bool allMonitored) 0074 { 0075 Q_UNUSED(allMonitored) 0076 } 0077 void setMonitoredCollection(qlonglong id, bool monitored) 0078 { 0079 Q_UNUSED(id) 0080 Q_UNUSED(monitored) 0081 } 0082 void setMonitoredItem(qlonglong id, bool monitored) 0083 { 0084 Q_UNUSED(id) 0085 Q_UNUSED(monitored) 0086 } 0087 void setMonitoredResource(const QByteArray &resource, bool monitored) 0088 { 0089 Q_UNUSED(resource) 0090 Q_UNUSED(monitored) 0091 } 0092 void setMonitoredMimeType(const QString &mimeType, bool monitored) 0093 { 0094 Q_UNUSED(mimeType) 0095 Q_UNUSED(monitored) 0096 } 0097 void setIgnoredSession(const QByteArray &session, bool ignored) 0098 { 0099 Q_UNUSED(session) 0100 Q_UNUSED(ignored) 0101 } 0102 0103 void setSession(const QByteArray &session) 0104 { 0105 Q_UNUSED(session) 0106 } 0107 }; 0108 0109 class AKONADITESTFAKE_EXPORT FakeNotificationConnection : public Akonadi::Connection 0110 { 0111 Q_OBJECT 0112 0113 public: 0114 explicit FakeNotificationConnection(Akonadi::CommandBuffer *buffer) 0115 : Connection(Connection::NotificationConnection, "", buffer) 0116 , mBuffer(buffer) 0117 { 0118 } 0119 0120 ~FakeNotificationConnection() override 0121 { 0122 } 0123 0124 void emitNotify(const Akonadi::Protocol::ChangeNotificationPtr &ntf) 0125 { 0126 Akonadi::CommandBufferLocker locker(mBuffer); 0127 mBuffer->enqueue(3, ntf); 0128 } 0129 0130 private: 0131 Akonadi::CommandBuffer *mBuffer; 0132 }; 0133 0134 class FakeMonitorDependenciesFactory : public Akonadi::ChangeNotificationDependenciesFactory 0135 { 0136 public: 0137 FakeMonitorDependenciesFactory(FakeItemCache *itemCache_, FakeCollectionCache *collectionCache_) 0138 : Akonadi::ChangeNotificationDependenciesFactory() 0139 , itemCache(itemCache_) 0140 , collectionCache(collectionCache_) 0141 { 0142 } 0143 0144 Akonadi::Connection *createNotificationConnection(Akonadi::Session *parent, Akonadi::CommandBuffer *buffer) override 0145 { 0146 auto conn = new FakeNotificationConnection(buffer); 0147 addConnection(parent, conn); 0148 return conn; 0149 } 0150 0151 void destroyNotificationConnection(Akonadi::Session *parent, Akonadi::Connection *connection) override 0152 { 0153 Q_UNUSED(parent) 0154 delete connection; 0155 } 0156 0157 Akonadi::CollectionCache *createCollectionCache(int maxCapacity, Akonadi::Session *session) override 0158 { 0159 Q_UNUSED(maxCapacity) 0160 Q_UNUSED(session) 0161 return collectionCache; 0162 } 0163 0164 Akonadi::ItemCache *createItemCache(int maxCapacity, Akonadi::Session *session) override 0165 { 0166 Q_UNUSED(maxCapacity) 0167 Q_UNUSED(session) 0168 return itemCache; 0169 } 0170 0171 private: 0172 FakeItemCache *itemCache = nullptr; 0173 FakeCollectionCache *collectionCache = nullptr; 0174 };