KWeatherCore

pendingcap.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Han Young <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 #include "pendingcap.h"
7 #include "capparser.h"
8 #include <QNetworkReply>
9 namespace KWeatherCore
10 {
11 class PendingCAPPrivate : public QObject
12 {
13  Q_OBJECT
14 public:
15  PendingCAPPrivate(QNetworkReply *reply = nullptr, QObject *parent = nullptr);
16  bool isFinished = false;
17  QByteArray data;
18 Q_SIGNALS:
19  void networkError();
20  void finished();
21 
22 private:
23  void parseCAP(QNetworkReply *reply);
24 };
25 PendingCAPPrivate::PendingCAPPrivate(QNetworkReply *reply, QObject *parent)
26  : QObject(parent)
27 {
28  if (reply) {
29  connect(reply, &QNetworkReply::finished, this, [this, reply] {
30  this->parseCAP(reply);
31  });
32  connect(reply, &QNetworkReply::errorOccurred, this, &PendingCAPPrivate::networkError);
33  }
34 }
35 
36 void PendingCAPPrivate::parseCAP(QNetworkReply *reply)
37 {
38  reply->deleteLater();
39  if (reply->error()) {
40  qWarning() << "network error when fetching alerts:" << reply->errorString();
41  return;
42  }
43 
44  data = reply->readAll();
45  isFinished = true;
46  Q_EMIT finished();
47 }
48 std::unique_ptr<AlertEntry> PendingCAP::value() const
49 {
50  if (d->data.isEmpty()) {
51  return {};
52  }
53 
54  CAPParser parser(d->data);
55  return parser.parse();
56 }
57 PendingCAP::PendingCAP(QNetworkReply *reply)
58  : d(new PendingCAPPrivate(reply, this))
59 {
60  connect(d, &PendingCAPPrivate::finished, this, &PendingCAP::finished);
61  connect(d, &PendingCAPPrivate::networkError, this, &PendingCAP::networkError);
62 }
64 {
65  return d->isFinished;
66 }
67 }
68 #include "pendingcap.moc"
bool isFinished() const
isFinished if the call has finished
QString errorString() const const
Q_SIGNALSQ_SIGNALS
std::unique_ptr< AlertEntry > value() const
value pointer to the shared alerts data the pointer is nullptr until finished() raised ...
Definition: pendingcap.cpp:48
Q_OBJECTQ_OBJECT
QByteArray readAll()
void deleteLater()
void finished()
signals the call has finished
void errorOccurred(QNetworkReply::NetworkError code)
void finished()
signals the call has finished
bool isFinished() const
isFinished if the call has finished
Definition: pendingcap.cpp:63
void networkError()
indicate there is a network error
QNetworkReply::NetworkError error() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
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.