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 #include "fakeserverdata.h"
0008 
0009 #include "collectionfetchjob.h"
0010 #include "itemfetchjob.h"
0011 
0012 #include <QTimer>
0013 using namespace std::chrono_literals;
0014 
0015 FakeServerData::FakeServerData(EntityTreeModel *model, FakeSession *session, FakeMonitor *monitor, QObject *parent)
0016     : QObject(parent)
0017     , m_model(model)
0018     , m_nextCollectionId(1)
0019     , m_nextItemId(0)
0020     , m_nextTagId(1)
0021 {
0022     // can't use QueuedConnection here, because the Job might self-deleted before
0023     // the slot gets called
0024     connect(session, &FakeSession::jobAdded, this, [this](Akonadi::Job *job) {
0025         Collection::Id fetchColId = job->property("FetchCollectionId").toULongLong();
0026         QTimer::singleShot(0s, [this, fetchColId]() {
0027             jobAdded(fetchColId);
0028         });
0029     });
0030 }
0031 
0032 FakeServerData::FakeServerData(TagModel *model, FakeSession *session, FakeMonitor *monitor, QObject *parent)
0033     : QObject(parent)
0034     , m_model(model)
0035     , m_nextCollectionId(1)
0036     , m_nextItemId(0)
0037     , m_nextTagId(1)
0038 {
0039     connect(session, &FakeSession::jobAdded, this, [this](Akonadi::Job * /*unused*/) {
0040         QTimer::singleShot(0s, [this]() {
0041             jobAdded();
0042         });
0043     });
0044 }
0045 
0046 void FakeServerData::setCommands(const QList<FakeAkonadiServerCommand *> &list)
0047 {
0048     m_communicationQueue.clear();
0049     for (FakeAkonadiServerCommand *command : list) {
0050         m_communicationQueue << command;
0051     }
0052 }
0053 
0054 void FakeServerData::processNotifications()
0055 {
0056     while (!m_communicationQueue.isEmpty()) {
0057         FakeAkonadiServerCommand::Type respondTo = m_communicationQueue.head()->respondTo();
0058         if (respondTo == FakeAkonadiServerCommand::Notification) {
0059             FakeAkonadiServerCommand *command = m_communicationQueue.dequeue();
0060             command->doCommand();
0061             delete command;
0062         } else {
0063             return;
0064         }
0065     }
0066 }
0067 
0068 void FakeServerData::jobAdded(qint64 fetchColId)
0069 {
0070     returnEntities(fetchColId);
0071 }
0072 
0073 void FakeServerData::jobAdded()
0074 {
0075     while (!m_communicationQueue.isEmpty() && m_communicationQueue.head()->respondTo() == FakeAkonadiServerCommand::RespondToTagFetch) {
0076         returnTags();
0077     }
0078 
0079     processNotifications();
0080 }
0081 
0082 void FakeServerData::returnEntities(Collection::Id fetchColId)
0083 {
0084     if (!returnCollections(fetchColId)) {
0085         while (!m_communicationQueue.isEmpty() && m_communicationQueue.head()->respondTo() == FakeAkonadiServerCommand::RespondToItemFetch) {
0086             returnItems(fetchColId);
0087         }
0088     }
0089 
0090     processNotifications();
0091 }
0092 
0093 bool FakeServerData::returnCollections(Collection::Id fetchColId)
0094 {
0095     if (m_communicationQueue.isEmpty()) {
0096         return true;
0097     }
0098     FakeAkonadiServerCommand::Type commType = m_communicationQueue.head()->respondTo();
0099 
0100     Collection fetchCollection = m_communicationQueue.head()->fetchCollection();
0101 
0102     if (commType == FakeAkonadiServerCommand::RespondToCollectionFetch && fetchColId == fetchCollection.id()) {
0103         FakeAkonadiServerCommand *command = m_communicationQueue.dequeue();
0104         command->doCommand();
0105         if (!m_communicationQueue.isEmpty()) {
0106             returnEntities(fetchColId);
0107         }
0108         delete command;
0109         return true;
0110     }
0111     return false;
0112 }
0113 
0114 void FakeServerData::returnItems(Item::Id fetchColId)
0115 {
0116     FakeAkonadiServerCommand::Type commType = m_communicationQueue.head()->respondTo();
0117 
0118     if (commType == FakeAkonadiServerCommand::RespondToItemFetch) {
0119         FakeAkonadiServerCommand *command = m_communicationQueue.dequeue();
0120         command->doCommand();
0121         if (!m_communicationQueue.isEmpty()) {
0122             returnEntities(fetchColId);
0123         }
0124         delete command;
0125     }
0126 }
0127 
0128 void FakeServerData::returnTags()
0129 {
0130     FakeAkonadiServerCommand::Type commType = m_communicationQueue.head()->respondTo();
0131 
0132     if (commType == FakeAkonadiServerCommand::RespondToTagFetch) {
0133         FakeAkonadiServerCommand *command = m_communicationQueue.dequeue();
0134         command->doCommand();
0135         delete command;
0136     }
0137 }
0138 
0139 #include "moc_fakeserverdata.cpp"