KWeatherCore

weatherforecastsource.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 "weatherforecastsource.h"
9#include "kweathercore_p.h"
10#include "locationqueryresult.h"
11#include "weatherforecast.h"
12
13#include <KTimeZone>
14
15#include <QFile>
16#include <QJsonDocument>
17#include <QNetworkAccessManager>
18#include <QStandardPaths>
19
20#include <algorithm>
21
22namespace KWeatherCore
23{
24class WeatherForecastSourcePrivate
25{
26public:
27 QNetworkAccessManager *m_nam = nullptr;
28};
29
30WeatherForecastSource::WeatherForecastSource(QObject *parent)
31 : QObject(parent)
32 , d(new WeatherForecastSourcePrivate)
33{
34}
35
36WeatherForecastSource::~WeatherForecastSource() = default;
37
38PendingWeatherForecast *WeatherForecastSource::requestData(double latitude, double longitude)
39{
40 QFile cache(KWeatherCorePrivate::getCacheDirectory(latitude, longitude).path() + QStringLiteral("/cache.json"));
41 QString timezone;
42 // valid cache
43 if (cache.exists() && cache.open(QIODevice::ReadOnly)) {
44 auto weatherforecast = WeatherForecast::fromJson(QJsonDocument::fromJson(cache.readAll()).object());
45 timezone = weatherforecast.timezone();
46 if (weatherforecast.createdTime().secsTo(QDateTime::currentDateTime()) <= 3600) {
47 return new PendingWeatherForecast(weatherforecast);
48 }
49 }
50
51 if (timezone.isEmpty()) {
52 timezone = QString::fromUtf8(KTimeZone::fromLocation(latitude, longitude));
53 }
54
55 if (!d->m_nam) {
56 d->m_nam = new QNetworkAccessManager(this);
57 d->m_nam->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
58 d->m_nam->setStrictTransportSecurityEnabled(true);
59 d->m_nam->enableStrictTransportSecurityStore(true,
61 + QLatin1String("/org.kde.kweathercore/hsts/"));
62 }
63
64 return new PendingWeatherForecast(latitude, longitude, timezone, d->m_nam);
65}
66
67PendingWeatherForecast *WeatherForecastSource::requestData(const KWeatherCore::LocationQueryResult &result)
68{
69 return requestData(result.latitude(), result.longitude());
70}
71
72void WeatherForecastSource::setNetworkAccessManager(QNetworkAccessManager *nam)
73{
74 if (d->m_nam == nam) {
75 return;
76 }
77
78 if (d->m_nam->parent() == this) {
79 delete d->m_nam;
80 }
81 d->m_nam = nam;
82}
83}
84
85#include "moc_weatherforecastsource.cpp"
Class represents location query result.
The PendingWeatherForecast class contains the reply to an asynchronous weather forecast request.
KI18NLOCALEDATA_EXPORT const char * fromLocation(float latitude, float longitude)
QDateTime currentDateTime()
bool exists(const QString &fileName)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QString writableLocation(StandardLocation type)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:42 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.