weatherforecastmanager.h 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
    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
15
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19 20
*/

#ifndef WEATHERFORECASTMANAGER_H
#define WEATHERFORECASTMANAGER_H

21 22
#include "weathertile.h"

23
#include <QObject>
24
#include <QTimer>
25 26

#include <deque>
27
#include <unordered_map>
28 29 30 31 32 33 34 35
#include <vector>

class WeatherForecast;

class QNetworkAccessManager;
class QNetworkReply;
class QXmlStreamReader;

Edward Betts's avatar
Edward Betts committed
36
/** Access to weather forecast data based on geo coordinates. */
37 38 39 40 41 42 43
class WeatherForecastManager : public QObject
{
    Q_OBJECT
public:
    explicit WeatherForecastManager(QObject *parent = nullptr);
    ~WeatherForecastManager();

44 45 46
    /** Kill switch for network operations. */
    void setAllowNetworkAccess(bool enabled);

47 48 49 50
    /** Monitor the specified location for weather forecasts. */
    void monitorLocation(float latitude, float longitude);

    /** Get the forecast for the given time and location. */
51 52 53
    WeatherForecast forecast(float latitude, float longitude, const QDateTime &dt) const;
    /** Get the forecast for the give time range and location. */
    WeatherForecast forecast(float latitude, float longitude, const QDateTime &begin, const QDateTime &end) const;
54

55
    /** Time until when we have forecast data. */
56
    QDateTime maximumForecastTime(const QDate &today) const;
57

58 59 60 61 62
    /** Enable unit test mode.
     *  In this mode static forecast data is provided for all locations.
     */
    void setTestModeEnabled(bool testMode);

63
Q_SIGNALS:
64 65 66 67
    /** Updated when new forecast data has been retrieved. */
    void forecastUpdated();

private:
68 69
    friend class WeatherTest;

70 71 72 73 74 75
    void fetchTile(WeatherTile tile);
    void fetchNext();
    void tileDownloaded();
    QString cachePath(WeatherTile tile) const;
    void writeToCacheFile(QNetworkReply *reply) const;

76
    bool loadForecastData(WeatherTile tile) const;
77
    void mergeForecasts(std::vector<WeatherForecast> &forecasts) const;
78
    std::vector<WeatherForecast> parseForecast(QXmlStreamReader &reader, WeatherTile tile) const;
79 80
    WeatherForecast parseForecastElement(QXmlStreamReader &reader) const;

81 82 83 84
    void scheduleUpdate();
    void updateAll();
    void purgeCache();

85 86
    std::vector<WeatherTile> m_monitoredTiles;
    std::deque<WeatherTile> m_pendingTiles;
87
    mutable std::unordered_map<WeatherTile, std::vector<WeatherForecast>> m_forecastData;
88 89 90

    QNetworkAccessManager *m_nam = nullptr;
    QNetworkReply *m_pendingReply = nullptr;
91
    QTimer m_updateTimer;
92
    bool m_allowNetwork = false;
93
    bool m_testMode = false;
94 95 96
};

#endif // WEATHERFORECASTMANAGER_H