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 qDebug() << "fetching schedule for station:" << station << "code:" << stationCode;
65 const QString queryString =
66 stationCode.isEmpty() ? "stazione=" + station :
67 "codiceStazione=" + stationCode;
68 const QByteArray query(queryString.toLocal8Bit());
69 stationQueryReply = accessManager->post(request, query);
70 connect(stationQueryReply, SIGNAL(metaDataChanged()),
71 SLOT(onStationQueryMetadataChanged()));
72 connect(stationQueryReply, SIGNAL(finished()),
73 SLOT(onStationScheduleFetched()));
74 connect(stationQueryReply, SIGNAL(error(QNetworkReply::NetworkError)),
75 SLOT(onNetworkError(QNetworkReply::NetworkError)));
76 QStringList recentStations = settings->recentStations();
77 recentStations.push_front(station);
78 recentStations.removeDuplicates();
79 if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
80 recentStations.pop_back();
82 settings->setRecentStations(recentStations);
85 void DataProvider::updateStation()
87 Settings *settings = Settings::instance();
89 qDebug() << "updating station data";
90 if (!settings->recentStations().isEmpty()) {
91 fetchStationSchedule(settings->recentStations().front());
95 void DataProvider::onStationScheduleFetched()
97 disconnect(stationQueryReply);
99 QVariant httpStatus = stationQueryReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
100 if (httpStatus.isValid()) {
101 qDebug() << "Metadata changed, Http status is:" << httpStatus.toInt();
103 emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
104 stationQueryReply->deleteLater();
105 stationQueryReply = 0;
108 void DataProvider::onStationQueryMetadataChanged(void)
112 void DataProvider::onNetworkError(QNetworkReply::NetworkError errorCode)
115 case QNetworkReply::NoError:
116 qDebug() << "No Network error" << errorCode;
119 qDebug() << "Network error" << errorCode;