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/QtWebKitWidgets>
32 #include <QWebElement>
37 static QHash<int, QByteArray> roles;
39 StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) :
40 QAbstractListModel(parent),
45 DataProvider *provider = DataProvider::instance();
46 QHash<int, QByteArray> roles;
47 roles[TrainRole] = "train";
48 roles[DepartureStationRole] = "departureStation";
49 roles[DepartureTimeRole] = "departureTime";
50 roles[ArrivalStationRole] = "arrivalStation";
51 roles[ArrivalTimeRole] = "arrivalTime";
52 roles[DetailsUrlRole] = "detailsUrl";
53 roles[DelayRole] = "delay";
54 roles[DelayClassRole] = "delayClass";
55 roles[ExpectedPlatformRole] = "expectedPlatform";
56 roles[ActualPlatformRole] = "actualPlatform";
57 #if (QT_VERSION <= QT_VERSION_CHECK(5, 0, 0))
61 connect(provider, SIGNAL(stationScheduleReady(QByteArray,QUrl)),
62 this, SLOT(parse(QByteArray,QUrl)));
63 connect(provider, SIGNAL(error()),
64 this, SLOT(onNetworkError()));
65 Settings *settings = Settings::instance();
66 m_scheduleType = settings->showArrivalsPreferred() ? ArrivalSchedule : DepartureSchedule;
69 const QString &StationScheduleModel::name()
74 void StationScheduleModel::setName(const QString &name)
82 const QString &StationScheduleModel::code()
87 void StationScheduleModel::setCode(const QString &code)
95 const QString &StationScheduleModel::error()
100 void StationScheduleModel::setError(const QString &error)
102 if (error != m_error) {
108 QHash<int, QByteArray> StationScheduleModel::roleNames() const
113 StationScheduleModel::ScheduleType StationScheduleModel::type()
115 return m_scheduleType;
118 void StationScheduleModel::setType(StationScheduleModel::ScheduleType type)
120 if (type != m_scheduleType) {
121 emit layoutAboutToBeChanged();
123 m_scheduleType = type;
126 emit layoutChanged();
127 Settings *settings = Settings::instance();
128 settings->setShowArrivalsPreferred(m_scheduleType == ArrivalSchedule ? true : false);
132 static void parseDelayClass(const QWebElement &element, StationScheduleItem &item)
134 if (!element.isNull()) {
135 QWebElement image = element.findFirst("img");
136 if (!image.isNull()) {
138 QString imageName = image.attribute("src");
139 if (!imageName.isEmpty()) {
140 QRegExp delayClassRegexp("pallinoRit([0-9])\\.png");
141 int pos = delayClassRegexp.indexIn(imageName);
142 qDebug() << "regexp matched at pos:" << pos << "match:" << delayClassRegexp.cap(0);
143 delayClass = (pos >= 0) ? delayClassRegexp.cap(1).toInt() : 0;
145 item.setDelayClass(delayClass);
147 qDebug() << "img not found";
150 qDebug() << "div.bloccotreno not found";
154 static void parseDetailsUrl(const QWebElement &element, StationScheduleItem &item)
156 if (!element.isNull()) {
157 QWebElement link = element.findFirst("a");
158 QString url = link.attribute("href");
159 item.setDetailsUrl(url);
161 qDebug() << "link not found";
165 static void parseTrain(const QString &text, StationScheduleItem &item)
167 QRegExp filter("^(Per|Da) (.*)\\n"
169 "Binario Previsto: (.*)\n"
170 "Binario Reale: (.*)\n"
172 int pos = filter.indexIn(text);
174 if (filter.cap(1) == "Per") {
175 item.setDepartureStation(filter.cap(2));
176 item.setDepartureTime(filter.cap(3));
178 item.setArrivalStation(filter.cap(2));
179 item.setArrivalTime(filter.cap(3));
181 item.setDelay(filter.cap(6));
182 item.setExpectedPlatform(filter.cap(4));
183 item.setActualPlatform(filter.cap(5));
185 qDebug() << "could not parse" << text;
189 StationScheduleItem parseResult(const QWebElement &result)
191 StationScheduleItem item;
193 QWebElement current = result.findFirst("h2");
194 if (!current.isNull()) {
195 item.setTrain(current.toPlainText());
197 parseDetailsUrl(result, item);
198 current = result.findFirst("div.bloccotreno");
199 parseDelayClass(current, item);
200 QString rawText = current.toPlainText();
201 parseTrain(rawText, item);
203 qDebug() << "train:" << item.train();
204 qDebug() << "delayClass:" << item.delayClass();
205 qDebug() << "detailsUrl:" << item.detailsUrl();
206 qDebug() << "departureStation:" << item.departureStation();
207 qDebug() << "departureTime:" << item.departureTime();
208 qDebug() << "arrivalStation:" << item.arrivalStation();
209 qDebug() << "arrivalTime:" << item.arrivalTime();
210 qDebug() << "expectedPlatform:" << item.expectedPlatform();
211 qDebug() << "actualPlatform:" << item.actualPlatform();
212 qDebug() << "delay:" << item.delay();
216 void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUrl)
219 qDebug() << "--- start of query result --- cut here ------";
220 qDebug() << QString::fromUtf8(htmlReply.constData());
221 qDebug() << "--- end of query result ----- cut here ------";
223 emit layoutAboutToBeChanged();
225 #if (QT_VERSION <= QT_VERSION_CHECK(5, 0, 0))
227 page.mainFrame()->setContent(htmlReply, "text/html", baseUrl);
228 QWebElement doc = page.mainFrame()->documentElement();
230 // Check if the page is reporting an error before parsing it
231 QWebElement errorElement = doc.findFirst("span.errore");
232 if (!errorElement.isNull()) {
233 m_departureSchedules.clear();
234 m_arrivalSchedules.clear();
235 QString errorText = errorElement.toPlainText().trimmed();
236 if (errorText == "localita' non trovata") {
237 setError(tr("Unknown station"));
239 setError(tr("Unknown error"));
241 qDebug() << "error:" << error();
244 // Find the first div
245 QWebElement current = doc.findFirst("div");
247 qDebug() << "skipping to the departures";
248 // Skip to the first div of class corpocentrale, which contains the first
249 // departure-related contents
250 while (!current.classes().contains("corpocentrale")) {
251 current = current.nextSibling();
252 qDebug() << "skipping to the next element";
253 if (current.isNull())
256 // Mark every div as a departure class element; the next corpocentrale
257 // marks the start of the arrivals section
258 qDebug() << "marking departures";
260 if (current.classes().contains("bloccorisultato")) {
261 StationScheduleItem schedule = parseResult(current);
262 if (schedule.isValid()) {
263 m_departureSchedules << schedule;
266 current = current.nextSibling();
267 qDebug() << "marking as departures";
268 if (current.isNull())
270 } while (!current.classes().contains("corpocentrale"));
272 // Mark everything as an arrival, until reaching the footer
273 while (!current.classes().contains("footer")) {
274 if (current.classes().contains("bloccorisultato")) {
275 StationScheduleItem schedule = parseResult(current);
276 if (schedule.isValid()) {
277 m_arrivalSchedules << schedule;
280 current = current.nextSibling();
281 qDebug() << "marking as arrival";
282 if (current.isNull())
287 emit layoutChanged();
290 void StationScheduleModel::onNetworkError()
292 qDebug()<< "Station Schedule Model got a Network Error";
293 m_error = tr("Network error");
297 void StationScheduleModel::fetch(const QString &name, const QString &code)
299 DataProvider *provider = DataProvider::instance();
301 if (!error().isEmpty())
303 m_departureSchedules.clear();
304 m_arrivalSchedules.clear();
305 provider->fetchStationSchedule(name, code);
310 int StationScheduleModel::rowCount(const QModelIndex &parent) const
313 if (m_scheduleType == DepartureSchedule) {
314 return m_departureSchedules.count();
316 return m_arrivalSchedules.count();
320 QVariant StationScheduleModel::data(const QModelIndex &index, int role) const
322 if (!index.isValid()) {
325 const QList<StationScheduleItem> &schedules =
326 (m_scheduleType == DepartureSchedule) ? m_departureSchedules : m_arrivalSchedules;
327 if (index.row() < 0 || index.row() >= schedules.count()) {
330 StationScheduleItem item = schedules[index.row()];
332 case Qt::DisplayRole:
334 return QVariant::fromValue(item.train());
335 case DepartureStationRole:
336 return QVariant::fromValue(item.departureStation());
337 case DepartureTimeRole:
338 return QVariant::fromValue(item.departureTime());
339 case ArrivalStationRole:
340 return QVariant::fromValue(item.arrivalStation());
341 case ArrivalTimeRole:
342 return QVariant::fromValue(item.arrivalTime());
344 return QVariant::fromValue(item.detailsUrl());
346 return QVariant::fromValue(item.delay());
348 return QVariant::fromValue(item.delayClass());
349 case ExpectedPlatformRole:
350 return QVariant::fromValue(item.expectedPlatform());
351 case ActualPlatformRole:
352 return QVariant::fromValue(item.actualPlatform());
354 return QVariant::fromValue(QString("Unknown role requested"));