File indexing completed on 2024-11-03 13:05:05

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "reply.h"
0008 #include "reply_p.h"
0009 #include "assetrepository_p.h"
0010 #include "datatypes/attributionutil_p.h"
0011 
0012 #include <QDebug>
0013 #include <QUrl>
0014 
0015 using namespace KPublicTransport;
0016 
0017 void ReplyPrivate::emitFinishedIfDone(Reply *q)
0018 {
0019     if (pendingOps == 0 && (!needToWaitForAssets() || AssetRepository::instance()->isQueueEmpty())) {
0020         finalizeResult();
0021         // delayed, as this is trigged from the backend settings results on us, which can be a multi-step process
0022         QMetaObject::invokeMethod(q, &Reply::finished, Qt::QueuedConnection);
0023     }
0024 }
0025 
0026 void ReplyPrivate::emitUpdated(Reply *q)
0027 {
0028     shouldClearError = true;
0029     // delayed, as this is trigged from immediate cache lookup in Manager, when signals are not yet connected
0030     QMetaObject::invokeMethod(q, &Reply::updated, Qt::QueuedConnection);
0031 }
0032 
0033 bool ReplyPrivate::needToWaitForAssets() const
0034 {
0035     return false;
0036 }
0037 
0038 Reply::Reply(ReplyPrivate *dd, QObject *parent)
0039     : QObject(parent)
0040     , d_ptr(dd)
0041 {
0042 }
0043 
0044 Reply::~Reply() = default;
0045 
0046 Reply::Error Reply::error() const
0047 {
0048     if (d_ptr->shouldClearError) {
0049         return Reply::NoError;
0050     }
0051     return d_ptr->error;
0052 }
0053 
0054 QString Reply::errorString() const
0055 {
0056     if (d_ptr->shouldClearError) {
0057         return {};
0058     }
0059     return d_ptr->errorMsg;
0060 }
0061 
0062 void Reply::addError(Reply::Error error, const QString &errorMsg)
0063 {
0064     d_ptr->error = error;
0065     d_ptr->errorMsg = errorMsg;
0066     d_ptr->pendingOps--;
0067     d_ptr->emitFinishedIfDone(this);
0068 }
0069 
0070 void Reply::setPendingOps(int ops)
0071 {
0072     Q_ASSERT(d_ptr->pendingOps <= -1);
0073     Q_ASSERT(ops >= 0);
0074     d_ptr->pendingOps = ops;
0075     if (ops == 0) {
0076         QMetaObject::invokeMethod(this, &Reply::finished, Qt::QueuedConnection);
0077     }
0078 
0079     if (d_ptr->needToWaitForAssets()) {
0080         connect(AssetRepository::instance(), &AssetRepository::downloadFinished, this, [this]() { d_ptr->emitFinishedIfDone(this); });
0081     }
0082 }
0083 
0084 const std::vector<Attribution>& Reply::attributions() const
0085 {
0086     return d_ptr->attributions;
0087 }
0088 
0089 std::vector<Attribution>&& Reply::takeAttributions()
0090 {
0091     return std::move(d_ptr->attributions);
0092 }
0093 
0094 void Reply::addAttributions(std::vector<Attribution>&& attributions)
0095 {
0096     AttributionUtil::merge(d_ptr->attributions, std::move(attributions));
0097 }
0098 
0099 void Reply::addAttribution(const Attribution &attr)
0100 {
0101     AttributionUtil::merge(d_ptr->attributions, attr);
0102 }
0103 
0104 void Reply::addAttributions(const std::vector<Attribution> &attributions)
0105 {
0106     AttributionUtil::merge(d_ptr->attributions, attributions);
0107 }