KWeatherCore

pendingalerts.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Han Young <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 #include "pendingalerts.h"
7 #include <QNetworkReply>
8 namespace KWeatherCore
9 {
10 class PendingAlertsPrivate : public QObject
11 {
12  Q_OBJECT
13 public:
14  PendingAlertsPrivate(const QJsonDocument &config, QNetworkReply *reply = nullptr, QObject *parent = nullptr);
15  AlertEntries alertEntries;
16  bool isFinished = false;
17 Q_SIGNALS:
18  void networkError();
19  void finished();
20 
21 private:
22  void parseAlerts(QNetworkReply *reply);
23  FeedParser *parser = nullptr;
24 };
25 PendingAlertsPrivate::PendingAlertsPrivate(const QJsonDocument &config, QNetworkReply *reply, QObject *parent)
26  : QObject(parent)
27  , parser(new FeedParser(config, this))
28 {
29  if (reply) {
30  connect(reply, &QNetworkReply::finished, this, [this, reply] {
31  this->parseAlerts(reply);
32  });
33  }
34 }
35 void PendingAlertsPrivate::parseAlerts(QNetworkReply *reply)
36 {
37  reply->deleteLater();
38  if (reply->error()) {
39  qWarning() << "network error when fetching alerts:" << reply->errorString();
40  Q_EMIT networkError();
41  return;
42  }
43  alertEntries = parser->parse(reply->readAll());
44  isFinished = true;
45  Q_EMIT finished();
46 }
47 PendingAlerts::PendingAlerts(const QJsonDocument &config, QNetworkReply *reply)
48  : d(new PendingAlertsPrivate(config, reply, this))
49 {
50  connect(d, &PendingAlertsPrivate::finished, this, &PendingAlerts::finished);
51  connect(d, &PendingAlertsPrivate::networkError, this, &PendingAlerts::networkError);
52 }
54 {
55  return d->isFinished;
56 }
57 AlertEntries PendingAlerts::value() const
58 {
59  return d->alertEntries;
60 }
61 }
62 #include "pendingalerts.moc"
bool isFinished() const
isFinished if the call has finished
QString errorString() const const
Q_SIGNALSQ_SIGNALS
void networkError()
indicate there is a network error
Q_OBJECTQ_OBJECT
QByteArray readAll()
void deleteLater()
void finished()
signals the call has finished
AlertEntries value() const
value pointer to the shared alerts data the pointer is nullptr until finished() raised ...
QNetworkReply::NetworkError error() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:05:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.