KWeatherCore

geotimezone.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Han Young <[email protected]>
3  * SPDX-FileCopyrightText: 2020 Devin Lin <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 #include "geotimezone.h"
9 #include <QJsonDocument>
10 #include <QNetworkAccessManager>
11 #include <QNetworkReply>
12 #include <QNetworkRequest>
13 #include <QUrlQuery>
14 namespace KWeatherCore
15 {
16 class GeoTimezonePrivate : public QObject
17 {
18  Q_OBJECT
19 public:
20  GeoTimezonePrivate(double lat, double lon, GeoTimezone *parent);
21 Q_SIGNALS:
22  void finished(const QString &timezone);
23  void networkErrorOccured();
24 private Q_SLOTS:
25  void downloadFinished(QNetworkReply *reply);
26 
27 private:
28  QNetworkAccessManager *m_manager = nullptr;
29 };
30 GeoTimezonePrivate::GeoTimezonePrivate(double lat, double lon, GeoTimezone *parent)
31  : QObject(parent)
32 {
33  m_manager = new QNetworkAccessManager(this);
34 
35  m_manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
36  m_manager->setStrictTransportSecurityEnabled(true);
37  m_manager->enableStrictTransportSecurityStore(true);
38 
39  QUrl url(QStringLiteral("http://api.geonames.org/timezoneJSON"));
41  query.addQueryItem(QStringLiteral("lat"), QString::number(lat));
42  query.addQueryItem(QStringLiteral("lng"), QString::number(lon));
43  query.addQueryItem(QStringLiteral("username"), QStringLiteral("kweatherdev"));
44  url.setQuery(query);
45  QNetworkRequest req(url);
46 
47  connect(m_manager, &QNetworkAccessManager::finished, this, &GeoTimezonePrivate::downloadFinished);
48  connect(this, &GeoTimezonePrivate::finished, parent, &GeoTimezone::finished);
49  connect(this, &GeoTimezonePrivate::networkErrorOccured, parent, &GeoTimezone::networkErrorOccured);
50  m_manager->get(req);
51 }
52 GeoTimezone::GeoTimezone(double lat, double lon, QObject *parent)
53  : QObject(parent)
54  , d(new GeoTimezonePrivate(lat, lon, this))
55 {
56 }
57 void GeoTimezonePrivate::downloadFinished(QNetworkReply *reply)
58 {
59  reply->deleteLater();
60  if (reply->error()) {
62  return;
63  }
64 
66  // if our api calls reached daily limit
67  if (doc[QStringLiteral("status")][QStringLiteral("value")].toInt() == 18) {
68  qWarning() << "api calls reached daily limit";
69  return;
70  }
71  Q_EMIT finished(doc[QStringLiteral("timezoneId")].toString());
72 }
73 }
74 #include "geotimezone.moc"
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
std::optional< QSqlQuery > query(const QString &queryStatement)
Q_SIGNALSQ_SIGNALS
void addQueryItem(const QString &key, const QString &value)
void networkErrorOccured()
networkError encounted network error
QString number(int n, int base)
Q_OBJECTQ_OBJECT
QByteArray readAll()
void deleteLater()
void finished(QNetworkReply *reply)
QNetworkReply::NetworkError error() const const
GeoTimezone(double latitude, double longitude, QObject *parent=nullptr)
GeoTimezone.
Definition: geotimezone.cpp:52
Q_SLOTSQ_SLOTS
Q_EMITQ_EMIT
void finished(const QString &timezone)
finished emit when the timezone has been obtained
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 19 2021 23:05:29 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.