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 <QJsonValue>
13#include <QNetworkAccessManager>
14#include <QNetworkReply>
15#include <QNetworkRequest>
16#include <QUrlQuery>
17
18namespace KWeatherCore
19{
20class GeoTimezonePrivate : public ReplyPrivate
21{
22public:
23 QString m_timezone;
24};
25
27 : Reply(new GeoTimezonePrivate, parent)
28{
30 QUrl url(QStringLiteral("https://secure.geonames.org/timezoneJSON"));
31 QUrlQuery query;
32 query.addQueryItem(QStringLiteral("lat"), QString::number(lat));
33 query.addQueryItem(QStringLiteral("lng"), QString::number(lon));
34 query.addQueryItem(QStringLiteral("username"), QStringLiteral("kweatherdev"));
35 url.setQuery(query);
36
37 QNetworkRequest req(url);
38 auto reply = nam->get(req);
39 connect(reply, &QNetworkReply::finished, this, [reply, this]() {
41 reply->deleteLater();
42 if (reply->error() != QNetworkReply::NoError) {
43 d->setError(Reply::NetworkError, reply->errorString());
44 } else {
45 const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
46 // if our api calls reached daily limit
47 if (doc[QLatin1String("status")][QLatin1String("value")].toInt() == 18) {
48 d->setError(Reply::RateLimitExceeded);
49 qWarning() << "api calls reached daily limit";
50 } else {
51 d->m_timezone = doc[QLatin1String("timezoneId")].toString();
52 }
53 }
55 });
56}
57
59{
60 Q_D(const GeoTimezone);
61 return d->m_timezone;
62}
63}
64
65#include "moc_geotimezone.cpp"
GeoTimezone(QNetworkAccessManager *nam, double latitude, double longitude, QObject *parent=nullptr)
GeoTimezone.
QString timezone() const
The result IANA timezone string.
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)
QObject(QObject *parent)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
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-2025 The KDE developers.
Generated on Fri Jan 31 2025 12:11:37 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.