KWeatherCore

geotimezone.cpp
1/*
2 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
3 * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "geotimezone.h"
9#include "reply_p.h"
10
11#include <QJsonDocument>
12#include <QNetworkAccessManager>
13#include <QNetworkReply>
14#include <QNetworkRequest>
15#include <QUrlQuery>
16
17namespace KWeatherCore
18{
19class GeoTimezonePrivate : public ReplyPrivate
20{
21public:
22 QString m_timezone;
23};
24
25GeoTimezone::GeoTimezone(QNetworkAccessManager *nam, double lat, double lon, QObject *parent)
26 : Reply(new GeoTimezonePrivate, parent)
27{
29 QUrl url(QStringLiteral("https://secure.geonames.org/timezoneJSON"));
30 QUrlQuery query;
31 query.addQueryItem(QStringLiteral("lat"), QString::number(lat));
32 query.addQueryItem(QStringLiteral("lng"), QString::number(lon));
33 query.addQueryItem(QStringLiteral("username"), QStringLiteral("kweatherdev"));
34 url.setQuery(query);
35
36 QNetworkRequest req(url);
37 auto reply = nam->get(req);
38 connect(reply, &QNetworkReply::finished, this, [reply, this]() {
40 reply->deleteLater();
41 if (reply->error() != QNetworkReply::NoError) {
42 d->setError(Reply::NetworkError, reply->errorString());
43 } else {
44 const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
45 // if our api calls reached daily limit
46 if (doc[QLatin1String("status")][QLatin1String("value")].toInt() == 18) {
47 d->setError(Reply::RateLimitExceeded);
48 qWarning() << "api calls reached daily limit";
49 } else {
50 d->m_timezone = doc[QLatin1String("timezoneId")].toString();
51 }
52 }
54 });
55}
56
58{
59 Q_D(const GeoTimezone);
60 return d->m_timezone;
61}
62}
63
64#include "moc_geotimezone.cpp"
Class to obtain timezone via coordinate.
Definition geotimezone.h:25
GeoTimezone(QNetworkAccessManager *nam, double latitude, double longitude, QObject *parent=nullptr)
GeoTimezone.
QString timezone() const
The result IANA timezone string.
Base class for all asynchronous jobs.
Definition reply.h:24
void finished()
Emitted once the job has been finished, either successfully or with an error.
@ NetworkError
Network operation failed.
Definition reply.h:32
@ RateLimitExceeded
Remote API rate limited exceeded.
Definition reply.h:33
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QNetworkReply * get(const QNetworkRequest &request)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString number(double n, char format, int precision)
void setQuery(const QString &query, ParsingMode mode)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.