summaryrefslogtreecommitdiffstats
path: root/weather/networkforecastsource.h
blob: 49f26c5fceacad663239144f5db87f2875883196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef NETWORKFORECASTSOURCE_H
#define NETWORKFORECASTSOURCE_H

#include "forecastdata.h"
#include "forecastsource.h"
#include <QObject>
#include <QMap>
#include <QStringList>
#include <QNetworkAccessManager>

class LocationRequestManager : public QObject
{
    Q_OBJECT
public:
    LocationRequestManager(QObject *parent = 0);
    int addRequest(const QString &query);

signals:
    void newLocationId(int reqId, const QString &locId);
    void locationIdQueryError(int reqId, const QString &query);

private slots:
    void receiveResponse(QNetworkReply *reply);

private:
    QMap<QString, int> m_requests;
    QNetworkAccessManager m_network;

    QString readResponse(const QString &query, QNetworkReply *reply);
};

class ForecastRequestmanager : public QObject
{
    Q_OBJECT
public:
    ForecastRequestmanager(QObject *parent = 0);

    int addRequest(const QString &locId);

public slots:
    void addRequest(int reqId, const QString &locId);

signals:
    void newForecastResponse(int reqId, YahooWeatherResponse *forecast);
    void forecastResponseError(int reqId);

private slots:
    void receiveResponse(QNetworkReply *reply);

private:
    QMap<QString, QList<int> > m_requests;
    QNetworkAccessManager m_network;

    YahooWeatherResponse *readResponse(const QString &query, QNetworkReply *locId);
    void doAddRequest(int reqId, const QString &locId);
};

class NetworkForecastSource : public ForecastSource
{
    Q_OBJECT
public:
    NetworkForecastSource(QObject *parent = 0);
    ~NetworkForecastSource();
    int getForecast(const QString &key, bool locationId);

private:
    LocationRequestManager m_locationManager;
    ForecastRequestmanager m_forecastManager;

private slots:
    void newForecastResponse(int reqId, YahooWeatherResponse *forecast);
    void forecastResponseError(int reqId);
};

#endif // NETWORKFORECASTSOURCE_H