3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
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.
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.
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.
22 #include "dataprovider.h"
24 #include "stationschedulemodel.h"
27 #include <QNetworkAccessManager>
28 #include <QNetworkReply>
29 #include <QNetworkRequest>
30 #include <QSharedPointer>
32 #include <QWebElement>
38 static const int RECENT_STATIONS_MAX_COUNT = 10;
42 DataProvider *DataProvider::instance()
44 static DataProvider *dataProvider = 0;
47 dataProvider = new DataProvider(0);
51 DataProvider::DataProvider(QObject *parent) :
53 accessManager(new QNetworkAccessManager(this)),
58 void DataProvider::fetchStationSchedule(const QString &station,
59 const QString &stationCode)
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();
83 settings->setRecentStations(recentStations);
86 void DataProvider::updateStation()
88 Settings *settings = Settings::instance();
90 qDebug() << "updating station data";
91 if (!settings->recentStations().isEmpty()) {
92 fetchStationSchedule(settings->recentStations().front());
96 void DataProvider::onStationScheduleFetched()
98 disconnect(stationQueryReply);
100 QVariant httpStatus = stationQueryReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
101 if (httpStatus.isValid()) {
102 qDebug() << "Metadata changed, Http status is:" << httpStatus.toInt();
104 emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
105 stationQueryReply->deleteLater();
106 stationQueryReply = 0;
109 void DataProvider::onStationQueryMetadataChanged(void)
113 void DataProvider::onNetworkError(QNetworkReply::NetworkError errorCode)
116 case QNetworkReply::NoError:
117 qDebug() << "No Network error" << errorCode;
120 qDebug() << "Network error" << errorCode;