Commit df0d0ab1 authored by Volker Krause's avatar Volker Krause

Persist and propagate settings

parent a2d556f2
......@@ -45,6 +45,7 @@ private slots:
void testForecastRetrieval()
{
WeatherForecastManager mgr;
mgr.setAllowNetworkAccess(true);
const auto now = QDateTime::currentDateTimeUtc().addSecs(1800);
auto fc = mgr.forecast(46.1, 7.78, now).value<WeatherForecast>();
QVERIFY(!fc.isValid());
......
......@@ -22,6 +22,7 @@ add_executable(itinerary-app
applicationcontroller.cpp
localizer.cpp
pkpassimageprovider.cpp
settings.cpp
qml.qrc
)
target_include_directories(itinerary-app PRIVATE ${CMAKE_BINARY_DIR})
......
......@@ -42,6 +42,8 @@ Kirigami.ScrollablePage {
}
QQC2.Switch {
id: weatherSwitch
checked: _settings.weatherForecastEnabled
onToggled: _settings.weatherForecastEnabled = checked
}
QQC2.Label {
Layout.columnSpan: 2
......
......@@ -25,6 +25,9 @@
#include "timelinemodel.h"
#include "pkpassimageprovider.h"
#include "reservationmanager.h"
#include "settings.h"
#include <weatherforecastmanager.h>
#include <KItinerary/CountryDb>
#include <KItinerary/Ticket>
......@@ -107,6 +110,11 @@ int main(int argc, char **argv)
timelineModel.setReservationManager(&resMgr);
ApplicationController appController;
Settings settings;
WeatherForecastManager weatherForecastMgr;
weatherForecastMgr.setAllowNetworkAccess(settings.weatherForecastEnabled());
QObject::connect(&settings, &Settings::weatherForecastEnabledChanged, &weatherForecastMgr, &WeatherForecastManager::setAllowNetworkAccess);
qmlRegisterUncreatableType<KPkPass::Barcode>("org.kde.pkpass", 1, 0, "Barcode", {});
qmlRegisterUncreatableType<KPkPass::Field>("org.kde.pkpass", 1, 0, "Field", {});
......@@ -127,6 +135,7 @@ int main(int argc, char **argv)
engine.rootContext()->setContextProperty(QStringLiteral("_reservationManager"), &resMgr);
engine.rootContext()->setContextProperty(QStringLiteral("_timelineModel"), &timelineModel);
engine.rootContext()->setContextProperty(QStringLiteral("_appController"), &appController);
engine.rootContext()->setContextProperty(QStringLiteral("_settings"), &settings);
engine.load(QStringLiteral(":/main.qml"));
for (const auto &file : parser.positionalArguments()) {
......
/*
Copyright (C) 2018 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "settings.h"
#include <QSettings>
Settings::Settings(QObject *parent)
: QObject(parent)
{
QSettings s;
s.beginGroup(QLatin1String("Settings"));
m_weatherEnabled = s.value(QLatin1String("WeatherForecastEnabled"), false).toBool();
}
Settings::~Settings() = default;
bool Settings::weatherForecastEnabled() const
{
return m_weatherEnabled;
}
void Settings::setWeatherForecastEnabled(bool enabled)
{
if (m_weatherEnabled == enabled) {
return;
}
m_weatherEnabled = enabled;
QSettings s;
s.beginGroup(QLatin1String("Settings"));
s.setValue(QLatin1String("WeatherForecastEnabled"), enabled);
emit weatherForecastEnabledChanged(enabled);
}
/*
Copyright (C) 2018 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QObject>
/** Application settings accessible by QML. */
class Settings : public QObject
{
Q_OBJECT
Q_PROPERTY(bool weatherForecastEnabled READ weatherForecastEnabled WRITE setWeatherForecastEnabled NOTIFY weatherForecastEnabledChanged)
public:
explicit Settings(QObject *parent = nullptr);
~Settings();
bool weatherForecastEnabled() const;
void setWeatherForecastEnabled(bool enabled);
signals:
void weatherForecastEnabledChanged(bool enabled);
private:
bool m_weatherEnabled = false;
};
#endif // SETTINGS_H
......@@ -45,6 +45,12 @@ WeatherForecastManager::WeatherForecastManager(QObject *parent)
WeatherForecastManager::~WeatherForecastManager() = default;
void WeatherForecastManager::setAllowNetworkAccess(bool enabled)
{
m_allowNetwork = enabled;
fetchNext();
}
void WeatherForecastManager::monitorLocation(float latitude, float longitude)
{
WeatherTile t{latitude, longitude};
......@@ -89,7 +95,7 @@ void WeatherForecastManager::fetchTile(WeatherTile tile)
void WeatherForecastManager::fetchNext()
{
if (m_pendingReply || m_pendingTiles.empty()) {
if (!m_allowNetwork || m_pendingReply || m_pendingTiles.empty()) {
return;
}
......
......@@ -38,6 +38,9 @@ public:
explicit WeatherForecastManager(QObject *parent = nullptr);
~WeatherForecastManager();
/** Kill switch for network operations. */
void setAllowNetworkAccess(bool enabled);
/** Monitor the specified location for weather forecasts. */
void monitorLocation(float latitude, float longitude);
// TODO unmonitor location(s)?
......@@ -65,6 +68,7 @@ private:
QNetworkAccessManager *m_nam = nullptr;
QNetworkReply *m_pendingReply = nullptr;
bool m_allowNetwork = false;
};
#endif // WEATHERFORECASTMANAGER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment