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 <QJsonObject>
18#include <QNetworkAccessManager>
19#include <QStandardPaths>
20
21#include <algorithm>
22
23namespace KWeatherCore
24{
25class WeatherForecastSourcePrivate
26{
27public:
28 QNetworkAccessManager *m_nam = nullptr;
29};
30
31WeatherForecastSource::WeatherForecastSource(QObject *parent)
32 : QObject(parent)
33 , d(new WeatherForecastSourcePrivate)
34{
35}
36
37WeatherForecastSource::~WeatherForecastSource() = default;
38
39PendingWeatherForecast *WeatherForecastSource::requestData(double latitude, double longitude)
40{
41 QFile cache(KWeatherCorePrivate::getCacheDirectory(latitude, longitude).path() + QStringLiteral("/cache.json"));
42 QString timezone;
43 // valid cache
44 if (cache.exists() && cache.open(QIODevice::ReadOnly)) {
45 auto weatherforecast = WeatherForecast::fromJson(QJsonDocument::fromJson(cache.readAll()).object());
46 timezone = weatherforecast.timezone();
47 if (weatherforecast.createdTime().secsTo(QDateTime::currentDateTime()) <= 3600) {
48 return new PendingWeatherForecast(weatherforecast);
49 }
50 }
51
52 if (timezone.isEmpty()) {
53 timezone = QString::fromUtf8(KTimeZone::fromLocation(latitude, longitude));
54 }
55
56 if (!d->m_nam) {
57 d->m_nam = new QNetworkAccessManager(this);
58 d->m_nam->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
59 d->m_nam->setStrictTransportSecurityEnabled(true);
60 d->m_nam->enableStrictTransportSecurityStore(true,
62 + QLatin1String("/org.kde.kweathercore/hsts/"));
63 }
64
65 return new PendingWeatherForecast(latitude, longitude, timezone, d->m_nam);
66}
67
68PendingWeatherForecast *WeatherForecastSource::requestData(const KWeatherCore::LocationQueryResult &result)
69{
70 return requestData(result.latitude(), result.longitude());
71}
72
73void WeatherForecastSource::setNetworkAccessManager(QNetworkAccessManager *nam)
74{
75 if (d->m_nam == nam) {
76 return;
77 }
78
79 if (d->m_nam->parent() == this) {
80 delete d->m_nam;
81 }
82 d->m_nam = nam;
83}
84}
85
86#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 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.