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>
31 #include <QWebElement>
36 static const int RECENT_STATIONS_MAX_COUNT = 10;
40 DataProvider *DataProvider::instance()
42 static DataProvider *dataProvider = 0;
45 dataProvider = new DataProvider(0);
49 DataProvider::DataProvider(QObject *parent) :
51 accessManager(new QNetworkAccessManager(this)),
56 void DataProvider::fetchStationSchedule(const QString &station,
57 const QString &stationCode)
59 QNetworkRequest request;
60 Settings *settings = Settings::instance();
61 request.setUrl(settings->queryBaseUrl() + "stazione");
62 qDebug() << "fetching schedule for station:" << station << "code:" << stationCode;
63 const QString queryString =
64 stationCode.isEmpty() ? "stazione=" + station :
65 "codiceStazione=" + stationCode;
66 const QByteArray query(queryString.toLocal8Bit());
67 stationQueryReply = accessManager->post(request, query);
68 connect(stationQueryReply, SIGNAL(metaDataChanged()),
69 SLOT(onStationQueryMetadataChanged()));
70 connect(stationQueryReply, SIGNAL(finished()),
71 SLOT(onStationScheduleFetched()));
72 connect(stationQueryReply, SIGNAL(error(QNetworkReply::NetworkError)),
73 SLOT(onNetworkError(QNetworkReply::NetworkError)));
74 QStringList recentStations = settings->recentStations();
75 recentStations.push_front(station);
76 recentStations.removeDuplicates();
77 if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
78 recentStations.pop_back();
80 settings->setRecentStations(recentStations);
83 void DataProvider::updateStation()
85 Settings *settings = Settings::instance();
87 qDebug() << "updating station data";
88 if (!settings->recentStations().isEmpty()) {
89 fetchStationSchedule(settings->recentStations().front());
93 void DataProvider::onStationScheduleFetched()
95 disconnect(stationQueryReply);
97 QVariant httpStatus = stationQueryReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
98 if (httpStatus.isValid()) {
99 qDebug() << "Metadata changed, Http status is:" << httpStatus.toInt();
101 emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
102 stationQueryReply->deleteLater();
103 stationQueryReply = 0;
106 void DataProvider::onStationQueryMetadataChanged(void)
110 void DataProvider::onNetworkError(QNetworkReply::NetworkError errorCode)
113 case QNetworkReply::NoError:
114 qDebug() << "No Network error" << errorCode;
117 qDebug() << "Network error" << errorCode;