Bump version to 0.9.0
[quandoparte] / application / dataprovider.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20 */
21
22 #include "dataprovider.h"
23 #include "settings.h"
24 #include "stationschedulemodel.h"
25
26 #include <QDebug>
27 #include <QNetworkAccessManager>
28 #include <QNetworkReply>
29 #include <QNetworkRequest>
30 #include <QSharedPointer>
31 #if 0
32 #include <QWebElement>
33 #include <QWebFrame>
34 #include <QWebPage>
35 #endif
36
37 // Constants
38 static const int RECENT_STATIONS_MAX_COUNT = 10;
39
40 // Class methods
41
42 DataProvider *DataProvider::instance()
43 {
44     static DataProvider *dataProvider = 0;
45
46     if (!dataProvider)
47         dataProvider = new DataProvider(0);
48     return dataProvider;
49 }
50
51 DataProvider::DataProvider(QObject *parent) :
52     QObject(parent),
53     accessManager(new QNetworkAccessManager(this)),
54     stationQueryReply(0)
55 {
56 }
57
58 void DataProvider::fetchStationSchedule(const QString &station,
59                                         const QString &stationCode)
60 {
61     QNetworkRequest request;
62     Settings *settings = Settings::instance();
63     request.setUrl(settings->queryBaseUrl() + "stazione");
64     request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
65     qDebug() << "fetching schedule for station:" << station << "code:" << stationCode;
66     const QString queryString =
67             stationCode.isEmpty() ? "stazione=" + station :
68                                     "codiceStazione=" + stationCode;
69     const QByteArray query(queryString.toLocal8Bit());
70     stationQueryReply = accessManager->post(request, query);
71     connect(stationQueryReply, SIGNAL(metaDataChanged()),
72             SLOT(onStationQueryMetadataChanged()));
73     connect(stationQueryReply, SIGNAL(finished()),
74             SLOT(onStationScheduleFetched()));
75     connect(stationQueryReply, SIGNAL(error(QNetworkReply::NetworkError)),
76             SLOT(onNetworkError(QNetworkReply::NetworkError)));
77     QStringList recentStations = settings->recentStations();
78     recentStations.push_front(station);
79     recentStations.removeDuplicates();
80     if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
81         recentStations.pop_back();
82     }
83     settings->setRecentStations(recentStations);
84 }
85
86 void DataProvider::updateStation()
87 {
88     Settings *settings = Settings::instance();
89
90     qDebug() << "updating station data";
91     if (!settings->recentStations().isEmpty()) {
92         fetchStationSchedule(settings->recentStations().front());
93     }
94 }
95
96 void DataProvider::onStationScheduleFetched()
97 {
98     disconnect(stationQueryReply);
99
100     QVariant httpStatus = stationQueryReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
101     if (httpStatus.isValid()) {
102         qDebug() << "Metadata changed, Http status is:" << httpStatus.toInt();
103     }
104     emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
105     stationQueryReply->deleteLater();
106     stationQueryReply = 0;
107 }
108
109 void DataProvider::onStationQueryMetadataChanged(void)
110 {
111 }
112
113 void DataProvider::onNetworkError(QNetworkReply::NetworkError errorCode)
114 {
115     switch (errorCode) {
116     case QNetworkReply::NoError:
117         qDebug() << "No Network error" << errorCode;
118         break;
119     default:
120         qDebug() << "Network error" << errorCode;
121         emit error();
122         break;
123     }
124 }