File indexing completed on 2024-04-28 04:42:43

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Han Young <hanyoung@protonmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "pendingalerts.h"
0008 #include "reply_p.h"
0009 
0010 #include <QNetworkReply>
0011 
0012 namespace KWeatherCore
0013 {
0014 class PendingAlertsPrivate : public ReplyPrivate
0015 {
0016 public:
0017     AlertEntries alertEntries;
0018     FeedParser *parser = nullptr;
0019 };
0020 
0021 PendingAlerts::PendingAlerts(const QJsonDocument &config, QNetworkReply *reply, QObject *parent)
0022     : Reply(new PendingAlertsPrivate, parent)
0023 {
0024     Q_D(PendingAlerts);
0025     d->parser = new FeedParser(config, this);
0026     if (reply) {
0027         connect(reply, &QNetworkReply::finished, this, [this, reply] {
0028             Q_D(PendingAlerts);
0029             reply->deleteLater();
0030             if (reply->error() != QNetworkReply::NoError) {
0031                 qWarning() << "network error when fetching alerts:" << reply->errorString();
0032                 d->setError(PendingAlerts::NetworkError, reply->errorString());
0033             } else {
0034                 d->alertEntries = d->parser->parse(reply->readAll());
0035             }
0036             Q_EMIT finished();
0037         });
0038     }
0039 }
0040 
0041 PendingAlerts::~PendingAlerts() = default;
0042 
0043 AlertEntries PendingAlerts::value() const
0044 {
0045     Q_D(const PendingAlerts);
0046     return d->alertEntries;
0047 }
0048 }
0049 
0050 #include "moc_pendingalerts.cpp"