KWeatherCore

weatherforecastsource.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 "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 
22 namespace KWeatherCore
23 {
24 class WeatherForecastSourcePrivate
25 {
26 public:
27  QNetworkAccessManager *m_nam = nullptr;
28 };
29 
30 WeatherForecastSource::WeatherForecastSource(QObject *parent)
31  : QObject(parent)
32  , d(new WeatherForecastSourcePrivate)
33 {
34 }
35 
36 WeatherForecastSource::~WeatherForecastSource() = default;
37 
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 
68 {
69  return requestData(result.latitude(), result.longitude());
70 }
71 
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 }
static WeatherForecast fromJson(const QJsonObject &obj)
construct from json
QJsonObject object() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString fromUtf8(const char *str, int size)
virtual bool open(QIODevice::OpenMode mode) override
QDateTime currentDateTime()
QString writableLocation(QStandardPaths::StandardLocation type)
bool exists() const const
PendingWeatherForecast * requestData(double latitude, double longitude)
requestData
bool isEmpty() const const
void setNetworkAccessManager(QNetworkAccessManager *nam)
Set the network access manager to use for network operations.
Class represents location query result.
QByteArray readAll()
The PendingWeatherForecast class contains the reply to an asynchronous weather forecast request.
const KI18NLOCALEDATA_EXPORT char * fromLocation(float latitude, float longitude)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 22 2023 03:55:38 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.