File indexing completed on 2024-11-10 04:40:11

0001 /*
0002   SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QString>
0010 
0011 #include "akonaditestfake_export.h"
0012 #include "collection.h"
0013 #include "entitytreemodel.h"
0014 #include "item.h"
0015 #include "tag.h"
0016 #include "tagmodel.h"
0017 
0018 class FakeServerData;
0019 
0020 class AKONADITESTFAKE_EXPORT FakeAkonadiServerCommand : public QObject
0021 {
0022     Q_OBJECT
0023 public:
0024     enum Type {
0025         Notification,
0026         RespondToCollectionFetch,
0027         RespondToItemFetch,
0028         RespondToTagFetch,
0029     };
0030 
0031     FakeAkonadiServerCommand(Type type, FakeServerData *serverData);
0032 
0033     ~FakeAkonadiServerCommand() override = default;
0034 
0035     Type respondTo() const
0036     {
0037         return m_type;
0038     }
0039     Akonadi::Collection fetchCollection() const
0040     {
0041         return m_parentCollection;
0042     }
0043 
0044     Type m_type;
0045 
0046     virtual void doCommand() = 0;
0047 
0048 Q_SIGNALS:
0049     void emit_itemsFetched(const Akonadi::Item::List &list);
0050     void emit_collectionsFetched(const Akonadi::Collection::List &list);
0051     void emit_tagsFetched(const Akonadi::Tag::List &tags);
0052 
0053     void emit_monitoredCollectionMoved(const Akonadi::Collection &collection, const Akonadi::Collection &source, const Akonadi::Collection &target);
0054     void emit_monitoredCollectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent);
0055     void emit_monitoredCollectionRemoved(const Akonadi::Collection &collection);
0056     void emit_monitoredCollectionChanged(const Akonadi::Collection &collection);
0057 
0058     void emit_monitoredItemMoved(const Akonadi::Item &item, const Akonadi::Collection &source, const Akonadi::Collection &target);
0059     void emit_monitoredItemAdded(const Akonadi::Item &item, const Akonadi::Collection &parent);
0060     void emit_monitoredItemRemoved(const Akonadi::Item &item);
0061     void emit_monitoredItemChanged(const Akonadi::Item &item, const QSet<QByteArray> &parts);
0062 
0063     void emit_monitoredItemLinked(const Akonadi::Item &item, const Akonadi::Collection &collection);
0064     void emit_monitoredItemUnlinked(const Akonadi::Item &item, const Akonadi::Collection &collection);
0065 
0066     void emit_monitoredTagAdded(const Akonadi::Tag &tag);
0067     void emit_monitoredTagChanged(const Akonadi::Tag &tag);
0068     void emit_monitoredTagRemoved(const Akonadi::Tag &tag);
0069 
0070 protected:
0071     Akonadi::Collection getCollectionByDisplayName(const QString &displayName) const;
0072     Akonadi::Item getItemByDisplayName(const QString &displayName) const;
0073     Akonadi::Tag getTagByDisplayName(const QString &displayName) const;
0074 
0075     bool isItemSignal(const QByteArray &signature) const;
0076     bool isCollectionSignal(const QByteArray &signature) const;
0077     bool isTagSignal(const QByteArray &signature) const;
0078 
0079 protected:
0080     FakeServerData *m_serverData = nullptr;
0081     QAbstractItemModel *m_model = nullptr;
0082     Akonadi::Collection m_parentCollection;
0083     Akonadi::Tag m_parentTag;
0084     QHash<Akonadi::Collection::Id, Akonadi::Collection> m_collections;
0085     QHash<Akonadi::Item::Id, Akonadi::Item> m_items;
0086     QHash<Akonadi::Collection::Id, QList<Akonadi::Collection::Id>> m_childElements;
0087     QHash<Akonadi::Tag::Id, Akonadi::Tag> m_tags;
0088 
0089 private:
0090     void connectForwardingSignals();
0091 };
0092 
0093 class AKONADITESTFAKE_EXPORT FakeMonitorCommand : public FakeAkonadiServerCommand
0094 {
0095     Q_OBJECT
0096 public:
0097     explicit FakeMonitorCommand(FakeServerData *serverData)
0098         : FakeAkonadiServerCommand(Notification, serverData)
0099     {
0100     }
0101 };
0102 
0103 class AKONADITESTFAKE_EXPORT FakeCollectionMovedCommand : public FakeMonitorCommand
0104 {
0105     Q_OBJECT
0106 public:
0107     FakeCollectionMovedCommand(const QString &collection, const QString &source, const QString &target, FakeServerData *serverData)
0108         : FakeMonitorCommand(serverData)
0109         , m_collectionName(collection)
0110         , m_sourceName(source)
0111         , m_targetName(target)
0112     {
0113     }
0114 
0115     void doCommand() override;
0116 
0117 private:
0118     QString m_collectionName;
0119     QString m_sourceName;
0120     QString m_targetName;
0121 };
0122 
0123 class AKONADITESTFAKE_EXPORT FakeCollectionAddedCommand : public FakeMonitorCommand
0124 {
0125     Q_OBJECT
0126 public:
0127     FakeCollectionAddedCommand(const QString &collection, const QString &parent, FakeServerData *serverData)
0128         : FakeMonitorCommand(serverData)
0129         , m_collectionName(collection)
0130         , m_parentName(parent)
0131     {
0132     }
0133 
0134     void doCommand() override;
0135 
0136 private:
0137     QString m_collectionName;
0138     QString m_parentName;
0139 };
0140 
0141 class AKONADITESTFAKE_EXPORT FakeCollectionRemovedCommand : public FakeMonitorCommand
0142 {
0143     Q_OBJECT
0144 public:
0145     FakeCollectionRemovedCommand(const QString &collection, const QString &source, FakeServerData *serverData)
0146         : FakeMonitorCommand(serverData)
0147         , m_collectionName(collection)
0148         , m_parentName(source)
0149     {
0150     }
0151 
0152     void doCommand() override;
0153 
0154 private:
0155     QString m_collectionName;
0156     QString m_parentName;
0157 };
0158 
0159 class AKONADITESTFAKE_EXPORT FakeCollectionChangedCommand : public FakeMonitorCommand
0160 {
0161     Q_OBJECT
0162 public:
0163     FakeCollectionChangedCommand(const QString &collection, const QString &parent, FakeServerData *serverData)
0164         : FakeMonitorCommand(serverData)
0165         , m_collectionName(collection)
0166         , m_parentName(parent)
0167     {
0168     }
0169 
0170     FakeCollectionChangedCommand(const Akonadi::Collection &collection, FakeServerData *serverData)
0171         : FakeMonitorCommand(serverData)
0172         , m_collection(collection)
0173     {
0174     }
0175 
0176     void doCommand() override;
0177 
0178 private:
0179     Akonadi::Collection m_collection;
0180     QString m_collectionName;
0181     QString m_parentName;
0182 };
0183 
0184 class AKONADITESTFAKE_EXPORT FakeItemMovedCommand : public FakeMonitorCommand
0185 {
0186     Q_OBJECT
0187 public:
0188     FakeItemMovedCommand(const QString &item, const QString &source, const QString &target, FakeServerData *serverData)
0189         : FakeMonitorCommand(serverData)
0190         , m_itemName(item)
0191         , m_sourceName(source)
0192         , m_targetName(target)
0193     {
0194     }
0195 
0196     void doCommand() override;
0197 
0198 private:
0199     QString m_itemName;
0200     QString m_sourceName;
0201     QString m_targetName;
0202 };
0203 
0204 class AKONADITESTFAKE_EXPORT FakeItemAddedCommand : public FakeMonitorCommand
0205 {
0206     Q_OBJECT
0207 public:
0208     FakeItemAddedCommand(const QString &item, const QString &parent, FakeServerData *serverData)
0209         : FakeMonitorCommand(serverData)
0210         , m_itemName(item)
0211         , m_parentName(parent)
0212     {
0213     }
0214 
0215     void doCommand() override;
0216 
0217 private:
0218     QString m_itemName;
0219     QString m_parentName;
0220 };
0221 
0222 class AKONADITESTFAKE_EXPORT FakeItemRemovedCommand : public FakeMonitorCommand
0223 {
0224     Q_OBJECT
0225 public:
0226     FakeItemRemovedCommand(const QString &item, const QString &parent, FakeServerData *serverData)
0227         : FakeMonitorCommand(serverData)
0228         , m_itemName(item)
0229         , m_parentName(parent)
0230     {
0231     }
0232 
0233     void doCommand() override;
0234 
0235 private:
0236     QString m_itemName;
0237     QString m_parentName;
0238 };
0239 
0240 class AKONADITESTFAKE_EXPORT FakeItemChangedCommand : public FakeMonitorCommand
0241 {
0242     Q_OBJECT
0243 public:
0244     FakeItemChangedCommand(const QString &item, const QString &parent, FakeServerData *serverData)
0245         : FakeMonitorCommand(serverData)
0246         , m_itemName(item)
0247         , m_parentName(parent)
0248     {
0249     }
0250 
0251     void doCommand() override;
0252 
0253 private:
0254     QString m_itemName;
0255     QString m_parentName;
0256 };
0257 
0258 class AKONADITESTFAKE_EXPORT FakeTagAddedCommand : public FakeMonitorCommand
0259 {
0260     Q_OBJECT
0261 public:
0262     FakeTagAddedCommand(const QString &tag, const QString &parent, FakeServerData *serverData)
0263         : FakeMonitorCommand(serverData)
0264         , m_tagName(tag)
0265         , m_parentName(parent)
0266     {
0267     }
0268 
0269     void doCommand() override;
0270 
0271 private:
0272     QString m_tagName;
0273     QString m_parentName;
0274 };
0275 
0276 class AKONADITESTFAKE_EXPORT FakeTagChangedCommand : public FakeMonitorCommand
0277 {
0278     Q_OBJECT
0279 public:
0280     FakeTagChangedCommand(const QString &tag, const QString &parent, FakeServerData *serverData)
0281         : FakeMonitorCommand(serverData)
0282         , m_tagName(tag)
0283         , m_parentName(parent)
0284     {
0285     }
0286 
0287     void doCommand() override;
0288 
0289 private:
0290     QString m_tagName;
0291     QString m_parentName;
0292 };
0293 
0294 class AKONADITESTFAKE_EXPORT FakeTagMovedCommand : public FakeMonitorCommand
0295 {
0296     Q_OBJECT
0297 public:
0298     FakeTagMovedCommand(const QString &tag, const QString &oldParent, const QString &newParent, FakeServerData *serverData)
0299         : FakeMonitorCommand(serverData)
0300         , m_tagName(tag)
0301         , m_oldParent(oldParent)
0302         , m_newParent(newParent)
0303     {
0304     }
0305 
0306     void doCommand() override;
0307 
0308 private:
0309     QString m_tagName;
0310     QString m_oldParent;
0311     QString m_newParent;
0312 };
0313 
0314 class AKONADITESTFAKE_EXPORT FakeTagRemovedCommand : public FakeMonitorCommand
0315 {
0316     Q_OBJECT
0317 public:
0318     FakeTagRemovedCommand(const QString &tag, const QString &parent, FakeServerData *serverData)
0319         : FakeMonitorCommand(serverData)
0320         , m_tagName(tag)
0321         , m_parentName(parent)
0322     {
0323     }
0324 
0325     void doCommand() override;
0326 
0327 private:
0328     QString m_tagName;
0329     QString m_parentName;
0330 };
0331 
0332 class AKONADITESTFAKE_EXPORT FakeJobResponse : public FakeAkonadiServerCommand
0333 {
0334     Q_OBJECT
0335     struct Token {
0336         enum Type {
0337             Branch,
0338             Leaf,
0339         };
0340         Type type;
0341         QString content;
0342     };
0343 
0344 public:
0345     FakeJobResponse(const Akonadi::Collection &parentCollection, Type respondTo, FakeServerData *serverData)
0346         : FakeAkonadiServerCommand(respondTo, serverData)
0347     {
0348         m_parentCollection = parentCollection;
0349     }
0350 
0351     FakeJobResponse(const Akonadi::Tag &parentTag, Type respondTo, FakeServerData *serverData)
0352         : FakeAkonadiServerCommand(respondTo, serverData)
0353     {
0354         m_parentTag = parentTag;
0355     }
0356 
0357     void appendCollection(const Akonadi::Collection &collection)
0358     {
0359         m_collections.insert(collection.id(), collection);
0360         m_childElements[collection.parentCollection().id()].append(collection.id());
0361     }
0362     void appendItem(const Akonadi::Item &item)
0363     {
0364         m_items.insert(item.id(), item);
0365     }
0366 
0367     void appendTag(const Akonadi::Tag &tag)
0368     {
0369         m_tags.insert(tag.id(), tag);
0370     }
0371 
0372     void doCommand() override;
0373 
0374     static QList<FakeAkonadiServerCommand *> interpret(FakeServerData *fakeServerData, const QString &input);
0375 
0376 private:
0377     static QList<FakeJobResponse *> parseTreeString(FakeServerData *fakeServerData, const QString &treeString);
0378     static QList<FakeJobResponse::Token> tokenize(const QString &treeString);
0379     static void parseEntityString(QList<FakeJobResponse *> &collectionResponseList,
0380                                   QHash<Akonadi::Collection::Id, FakeJobResponse *> &itemResponseMap,
0381                                   QList<FakeJobResponse *> &tagResponseList,
0382                                   Akonadi::Collection::List &recentCollections,
0383                                   Akonadi::Tag::List &recentTags,
0384                                   FakeServerData *fakeServerData,
0385                                   const QString &entityString,
0386                                   int depth);
0387 };