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 "stationschedulemodel.h"
24 #include "dataprovider.h"
29 #if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
30 #include <QtWebKitWidgets>
32 #include <QWebElement>
37 StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) :
38 QAbstractListModel(parent),
43 DataProvider *provider = DataProvider::instance();
44 connect(provider, SIGNAL(stationScheduleReady(QByteArray,QUrl)),
45 this, SLOT(parse(QByteArray,QUrl)));
46 connect(provider, SIGNAL(error()),
47 this, SLOT(onNetworkError()));
48 Settings *settings = Settings::instance();
49 m_scheduleType = settings->showArrivalsPreferred() ? ArrivalSchedule : DepartureSchedule;
50 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
51 setRoleNames(roleNames());
55 const QString &StationScheduleModel::name()
60 void StationScheduleModel::setName(const QString &name)
68 const QString &StationScheduleModel::code()
73 void StationScheduleModel::setCode(const QString &code)
81 const QString &StationScheduleModel::error()
86 void StationScheduleModel::setError(const QString &error)
88 if (error != m_error) {
94 QHash<int, QByteArray> StationScheduleModel::roleNames() const
96 QHash<int, QByteArray> roles;
97 roles[TrainRole] = "train";
98 roles[DepartureStationRole] = "departureStation";
99 roles[DepartureTimeRole] = "departureTime";
100 roles[ArrivalStationRole] = "arrivalStation";
101 roles[ArrivalTimeRole] = "arrivalTime";
102 roles[DetailsUrlRole] = "detailsUrl";
103 roles[DelayRole] = "delay";
104 roles[DelayClassRole] = "delayClass";
105 roles[ExpectedPlatformRole] = "expectedPlatform";
106 roles[ActualPlatformRole] = "actualPlatform";
111 StationScheduleModel::ScheduleType StationScheduleModel::type()
113 return m_scheduleType;
116 void StationScheduleModel::setType(StationScheduleModel::ScheduleType type)
118 if (type != m_scheduleType) {
119 emit layoutAboutToBeChanged();
121 m_scheduleType = type;
124 emit layoutChanged();
125 Settings *settings = Settings::instance();
126 settings->setShowArrivalsPreferred(m_scheduleType == ArrivalSchedule ? true : false);
130 static void parseDelayClass(const QWebElement &element, StationScheduleItem &item)
132 if (!element.isNull()) {
133 QWebElement image = element.findFirst("img");
134 if (!image.isNull()) {
136 QString imageName = image.attribute("src");
137 if (!imageName.isEmpty()) {
138 QRegExp delayClassRegexp("pallinoRit([0-9])\\.png");
139 int pos = delayClassRegexp.indexIn(imageName);
140 qDebug() << "regexp matched at pos:" << pos << "match:" << delayClassRegexp.cap(0);
141 delayClass = (pos >= 0) ? delayClassRegexp.cap(1).toInt() : 0;
143 item.setDelayClass(delayClass);
145 qDebug() << "img not found";
148 qDebug() << "div.bloccotreno not found";
152 static void parseDetailsUrl(const QWebElement &element, StationScheduleItem &item)
154 if (!element.isNull()) {
155 QWebElement link = element.findFirst("a");
156 QString url = link.attribute("href");
157 item.setDetailsUrl(url);
159 qDebug() << "link not found";
163 static void parseTrain(const QString &text, StationScheduleItem &item)
165 QRegExp filter("^(Per|Da) (.*)\\n"
167 "Binario Previsto: (.*)\n"
168 "Binario Reale: (.*)\n"
170 int pos = filter.indexIn(text);
172 if (filter.cap(1) == "Per") {
173 item.setDepartureStation(filter.cap(2));
174 item.setDepartureTime(filter.cap(3));
176 item.setArrivalStation(filter.cap(2));
177 item.setArrivalTime(filter.cap(3));
179 item.setDelay(filter.cap(6));
180 item.setExpectedPlatform(filter.cap(4));
181 item.setActualPlatform(filter.cap(5));
183 qDebug() << "could not parse" << text;
187 StationScheduleItem parseResult(const QWebElement &result)
189 StationScheduleItem item;
191 QWebElement current = result.findFirst("h2");
192 if (!current.isNull()) {
193 item.setTrain(current.toPlainText());
195 parseDetailsUrl(result, item);
196 current = result.findFirst("div.bloccotreno");
197 parseDelayClass(current, item);
198 QString rawText = current.toPlainText();
199 parseTrain(rawText, item);
201 qDebug() << "train:" << item.train();
202 qDebug() << "delayClass:" << item.delayClass();
203 qDebug() << "detailsUrl:" << item.detailsUrl();
204 qDebug() << "departureStation:" << item.departureStation();
205 qDebug() << "departureTime:" << item.departureTime();
206 qDebug() << "arrivalStation:" << item.arrivalStation();
207 qDebug() << "arrivalTime:" << item.arrivalTime();
208 qDebug() << "expectedPlatform:" << item.expectedPlatform();
209 qDebug() << "actualPlatform:" << item.actualPlatform();
210 qDebug() << "delay:" << item.delay();
214 void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUrl)
217 qDebug() << "--- start of query result --- cut here ------";
218 qDebug() << QString::fromUtf8(htmlReply.constData());
219 qDebug() << "--- end of query result ----- cut here ------";
221 emit layoutAboutToBeChanged();
224 page.mainFrame()->setContent(htmlReply, "text/html", baseUrl);
225 QWebElement doc = page.mainFrame()->documentElement();
227 // Check if the page is reporting an error before parsing it
228 QWebElement errorElement = doc.findFirst("span.errore");
229 if (!errorElement.isNull()) {
230 m_departureSchedules.clear();
231 m_arrivalSchedules.clear();
232 QString errorText = errorElement.toPlainText().trimmed();
233 if (errorText == "localita' non trovata") {
234 setError(tr("Unknown station"));
236 setError(tr("Unknown error"));
238 qDebug() << "error:" << error();
241 // Find the first div
242 QWebElement current = doc.findFirst("div");
244 qDebug() << "skipping to the departures";
245 // Skip to the first div of class corpocentrale, which contains the first
246 // departure-related contents
247 while (!current.classes().contains("corpocentrale")) {
248 current = current.nextSibling();
249 qDebug() << "skipping to the next element";
250 if (current.isNull())
253 // Mark every div as a departure class element; the next corpocentrale
254 // marks the start of the arrivals section
255 qDebug() << "marking departures";
257 if (current.classes().contains("bloccorisultato")) {
258 StationScheduleItem schedule = parseResult(current);
259 if (schedule.isValid()) {
260 m_departureSchedules << schedule;
263 current = current.nextSibling();
264 qDebug() << "marking as departures";
265 if (current.isNull())
267 } while (!current.classes().contains("corpocentrale"));
269 // Mark everything as an arrival, until reaching the footer
270 while (!current.classes().contains("footer")) {
271 if (current.classes().contains("bloccorisultato")) {
272 StationScheduleItem schedule = parseResult(current);
273 if (schedule.isValid()) {
274 m_arrivalSchedules << schedule;
277 current = current.nextSibling();
278 qDebug() << "marking as arrival";
279 if (current.isNull())
283 emit layoutChanged();
286 void StationScheduleModel::onNetworkError()
288 qDebug()<< "Station Schedule Model got a Network Error";
289 m_error = tr("Network error");
293 void StationScheduleModel::fetch(const QString &name, const QString &code)
295 DataProvider *provider = DataProvider::instance();
297 if (!error().isEmpty())
299 m_departureSchedules.clear();
300 m_arrivalSchedules.clear();
301 provider->fetchStationSchedule(name, code);
306 int StationScheduleModel::rowCount(const QModelIndex &parent) const
309 if (m_scheduleType == DepartureSchedule) {
310 return m_departureSchedules.count();
312 return m_arrivalSchedules.count();
316 QVariant StationScheduleModel::data(const QModelIndex &index, int role) const
318 if (!index.isValid()) {
321 const QList<StationScheduleItem> &schedules =
322 (m_scheduleType == DepartureSchedule) ? m_departureSchedules : m_arrivalSchedules;
323 if (index.row() < 0 || index.row() >= schedules.count()) {
326 StationScheduleItem item = schedules[index.row()];
328 case Qt::DisplayRole:
330 return QVariant::fromValue(item.train());
331 case DepartureStationRole:
332 return QVariant::fromValue(item.departureStation());
333 case DepartureTimeRole:
334 return QVariant::fromValue(item.departureTime());
335 case ArrivalStationRole:
336 return QVariant::fromValue(item.arrivalStation());
337 case ArrivalTimeRole:
338 return QVariant::fromValue(item.arrivalTime());
340 return QVariant::fromValue(item.detailsUrl());
342 return QVariant::fromValue(item.delay());
344 return QVariant::fromValue(item.delayClass());
345 case ExpectedPlatformRole:
346 return QVariant::fromValue(item.expectedPlatform());
347 case ActualPlatformRole:
348 return QVariant::fromValue(item.actualPlatform());
350 return QVariant::fromValue(QString("Unknown role requested"));