File indexing completed on 2024-05-12 04:42:51

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "abstractquerymodel.h"
0008 #include "abstractquerymodel_p.h"
0009 #include "assetrepository_p.h"
0010 #include "datatypes/attributionutil_p.h"
0011 
0012 #include <KPublicTransport/Attribution>
0013 #include <KPublicTransport/Manager>
0014 #include <KPublicTransport/Reply>
0015 
0016 #include <QDebug>
0017 #include <QTimer>
0018 
0019 using namespace KPublicTransport;
0020 
0021 AbstractQueryModelPrivate::~AbstractQueryModelPrivate() = default;
0022 
0023 void AbstractQueryModelPrivate::setLoading(bool l)
0024 {
0025     if (m_loading == l) {
0026         return;
0027     }
0028     m_loading = l;
0029     Q_EMIT q_ptr->loadingChanged();
0030 }
0031 
0032 void AbstractQueryModelPrivate::setErrorMessage(const QString &msg)
0033 {
0034     if (m_errorMessage == msg) {
0035         return;
0036     }
0037     m_errorMessage = msg;
0038     Q_EMIT q_ptr->errorMessageChanged();
0039 }
0040 
0041 void AbstractQueryModelPrivate::monitorReply(Reply *reply)
0042 {
0043     m_reply = reply;
0044     QObject::connect(reply, &Reply::finished, q_ptr, [this, reply]() {
0045         setLoading(false);
0046         reply->deleteLater();
0047         m_reply = nullptr;
0048         if (reply->error() == KPublicTransport::Reply::NoError) {
0049             AttributionUtil::merge(m_attributions, std::move(reply->takeAttributions()));
0050             Q_EMIT q_ptr->attributionsChanged();
0051         } else {
0052             setErrorMessage(reply->errorString());
0053         }
0054     });
0055 }
0056 
0057 void AbstractQueryModelPrivate::query()
0058 {
0059     if (!m_manager) {
0060         return;
0061     }
0062 
0063     q_ptr->cancel();
0064     m_queryTimer.start(m_queryDelay);
0065 }
0066 
0067 
0068 AbstractQueryModel::AbstractQueryModel(AbstractQueryModelPrivate* dd, QObject* parent)
0069     : QAbstractListModel(parent)
0070     , d_ptr(dd)
0071 {
0072     d_ptr->q_ptr = this;
0073 
0074     d_ptr->m_queryTimer.setSingleShot(true);
0075     connect(&d_ptr->m_queryTimer, &QTimer::timeout, this, [this]() {
0076         clear();
0077         d_ptr->doQuery();
0078     });
0079 
0080     connect(AssetRepository::instance(), &AssetRepository::downloadFinished, this, [this]() {
0081         const auto rows = rowCount();
0082         if (rows > 0) {
0083             Q_EMIT dataChanged(index(0, 0), index(rows - 1, 0));
0084         }
0085     });
0086 }
0087 
0088 AbstractQueryModel::~AbstractQueryModel() = default;
0089 
0090 Manager* AbstractQueryModel::manager() const
0091 {
0092     return d_ptr->m_manager;
0093 }
0094 
0095 void AbstractQueryModel::setManager(Manager *mgr)
0096 {
0097     if (d_ptr->m_manager == mgr) {
0098         return;
0099     }
0100 
0101     d_ptr->m_manager = mgr;
0102     Q_EMIT managerChanged();
0103     d_ptr->query();
0104 }
0105 
0106 bool AbstractQueryModel::isLoading() const
0107 {
0108     return d_ptr->m_loading;
0109 }
0110 
0111 QString AbstractQueryModel::errorMessage() const
0112 {
0113     return d_ptr->m_errorMessage;
0114 }
0115 
0116 const std::vector<Attribution>& AbstractQueryModel::attributions() const
0117 {
0118     return d_ptr->m_attributions;
0119 }
0120 
0121 QVariantList AbstractQueryModel::attributionsVariant() const
0122 {
0123     QVariantList l;
0124     l.reserve(d_ptr->m_attributions.size());
0125     std::transform(d_ptr->m_attributions.begin(), d_ptr->m_attributions.end(), std::back_inserter(l), [](const auto &attr) { return QVariant::fromValue(attr); });
0126     return l;
0127 }
0128 
0129 void AbstractQueryModel::cancel()
0130 {
0131     d_ptr->setLoading(false);
0132     delete d_ptr->m_reply;
0133     d_ptr->m_reply = nullptr;
0134 }
0135 
0136 void AbstractQueryModel::clear()
0137 {
0138     cancel();
0139     if (rowCount() > 0) {
0140         beginResetModel();
0141         d_ptr->doClearResults();
0142         endResetModel();
0143     }
0144 
0145     if (!d_ptr->m_attributions.empty()) {
0146         d_ptr->m_attributions.clear();
0147         Q_EMIT attributionsChanged();
0148     }
0149 
0150     d_ptr->setErrorMessage({});
0151 }
0152 
0153 #include "moc_abstractquerymodel.moc"