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),
42 DataProvider *provider = DataProvider::instance();
43 connect(provider, SIGNAL(stationScheduleReady(QByteArray,QUrl)),
44 this, SLOT(parse(QByteArray,QUrl)));
45 connect(provider, SIGNAL(error()),
46 this, SLOT(onNetworkError()));
47 Settings *settings = Settings::instance();
48 m_scheduleType = settings->showArrivalsPreferred() ? ArrivalSchedule : DepartureSchedule;
49 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
50 setRoleNames(roleNames());
54 const QString &StationScheduleModel::name()
59 void StationScheduleModel::setName(const QString &name)
67 const QString &StationScheduleModel::code()
72 void StationScheduleModel::setCode(const QString &code)
80 const QString &StationScheduleModel::error()
85 void StationScheduleModel::setError(const QString &error)
87 if (error != m_error) {
93 QHash<int, QByteArray> StationScheduleModel::roleNames() const
95 QHash<int, QByteArray> roles;
96 roles[TrainRole] = "train";
97 roles[DepartureStationRole] = "departureStation";
98 roles[DepartureTimeRole] = "departureTime";
99 roles[ArrivalStationRole] = "arrivalStation";
100 roles[ArrivalTimeRole] = "arrivalTime";
101 roles[DetailsUrlRole] = "detailsUrl";
102 roles[DelayRole] = "delay";
103 roles[DelayClassRole] = "delayClass";
104 roles[ExpectedPlatformRole] = "expectedPlatform";
105 roles[ActualPlatformRole] = "actualPlatform";
110 StationScheduleModel::ScheduleType StationScheduleModel::type()
112 return m_scheduleType;
115 void StationScheduleModel::setType(StationScheduleModel::ScheduleType type)
117 if (type != m_scheduleType) {
118 emit layoutAboutToBeChanged();
120 m_scheduleType = type;
123 emit layoutChanged();
124 Settings *settings = Settings::instance();
125 settings->setShowArrivalsPreferred(m_scheduleType == ArrivalSchedule ? true : false);
129 static void parseDelayClass(const QWebElement &element, StationScheduleItem &item)
131 if (!element.isNull()) {
132 QWebElement image = element.findFirst("img");
133 if (!image.isNull()) {
135 QString imageName = image.attribute("src");
136 if (!imageName.isEmpty()) {
137 QRegExp delayClassRegexp("pallinoRit([0-9])\\.png");
138 int pos = delayClassRegexp.indexIn(imageName);
139 qDebug() << "regexp matched at pos:" << pos << "match:" << delayClassRegexp.cap(0);
140 delayClass = (pos >= 0) ? delayClassRegexp.cap(1).toInt() : 0;
142 item.setDelayClass(delayClass);
144 qDebug() << "img not found";
147 qDebug() << "div.bloccotreno not found";
151 static void parseDetailsUrl(const QWebElement &element, StationScheduleItem &item)
153 if (!element.isNull()) {
154 QWebElement link = element.findFirst("a");
155 QString url = link.attribute("href");
156 item.setDetailsUrl(url);
158 qDebug() << "link not found";
162 static void parseTrain(const QString &text, StationScheduleItem &item)
164 QRegExp filter("^(Per|Da) (.*)\\n"
166 "Binario Previsto: (.*)\n"
167 "Binario Reale: (.*)\n"
169 int pos = filter.indexIn(text);
171 if (filter.cap(1) == "Per") {
172 item.setDepartureStation(filter.cap(2));
173 item.setDepartureTime(filter.cap(3));
175 item.setArrivalStation(filter.cap(2));
176 item.setArrivalTime(filter.cap(3));
178 QString delayDescription = filter.cap(6);
179 if (delayDescription == "in orario") {
180 item.setDelay(QObject::tr("On time"));
182 QRegExp delayRegExp("ritardo ([0-9]+)(| minuti)");
183 int pos = delayRegExp.indexIn(delayDescription);
185 item.setDelay(QString(QObject::tr("Delay: %1 minutes")).arg(delayRegExp.cap(1)));
187 // Does not match, let the user parse it
188 item.setDelay(delayDescription);
191 item.setExpectedPlatform(filter.cap(4));
192 item.setActualPlatform(filter.cap(5));
194 qDebug() << "could not parse" << text;
198 StationScheduleItem parseResult(const QWebElement &result)
200 StationScheduleItem item;
202 QWebElement current = result.findFirst("h2");
203 if (!current.isNull()) {
204 item.setTrain(current.toPlainText());
206 parseDetailsUrl(result, item);
207 current = result.findFirst("div.bloccotreno");
208 parseDelayClass(current, item);
209 QString rawText = current.toPlainText();
210 parseTrain(rawText, item);
212 qDebug() << "train:" << item.train();
213 qDebug() << "delayClass:" << item.delayClass();
214 qDebug() << "detailsUrl:" << item.detailsUrl();
215 qDebug() << "departureStation:" << item.departureStation();
216 qDebug() << "departureTime:" << item.departureTime();
217 qDebug() << "arrivalStation:" << item.arrivalStation();
218 qDebug() << "arrivalTime:" << item.arrivalTime();
219 qDebug() << "expectedPlatform:" << item.expectedPlatform();
220 qDebug() << "actualPlatform:" << item.actualPlatform();
221 qDebug() << "delay:" << item.delay();
225 void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUrl)
228 qDebug() << "--- start of query result --- cut here ------";
229 qDebug() << QString::fromUtf8(htmlReply.constData());
230 qDebug() << "--- end of query result ----- cut here ------";
232 emit layoutAboutToBeChanged();
235 page.mainFrame()->setContent(htmlReply, "text/html", baseUrl);
236 QWebElement doc = page.mainFrame()->documentElement();
238 // Check if the page is reporting an error before parsing it
239 QWebElement errorElement = doc.findFirst("span.errore");
240 if (!errorElement.isNull()) {
241 m_departureSchedules.clear();
242 m_arrivalSchedules.clear();
243 QString errorText = errorElement.toPlainText().trimmed();
244 if (errorText == "localita' non trovata") {
245 setError(tr("Unknown station"));
247 setError(tr("Unknown error"));
249 qDebug() << "error:" << error();
252 // Find the first div
253 QWebElement current = doc.findFirst("div");
255 qDebug() << "skipping to the departures";
256 // Skip to the first div of class corpocentrale, which contains the first
257 // departure-related contents
258 while (!current.classes().contains("corpocentrale")) {
259 current = current.nextSibling();
260 qDebug() << "skipping to the next element";
261 if (current.isNull())
264 // Mark every div as a departure class element; the next corpocentrale
265 // marks the start of the arrivals section
266 qDebug() << "marking departures";
268 if (current.classes().contains("bloccorisultato")) {
269 StationScheduleItem schedule = parseResult(current);
270 if (schedule.isValid()) {
271 m_departureSchedules << schedule;
274 current = current.nextSibling();
275 qDebug() << "marking as departures";
276 if (current.isNull())
278 } while (!current.classes().contains("corpocentrale"));
280 // Mark everything as an arrival, until reaching the footer
281 while (!current.classes().contains("footer")) {
282 if (current.classes().contains("bloccorisultato")) {
283 StationScheduleItem schedule = parseResult(current);
284 if (schedule.isValid()) {
285 m_arrivalSchedules << schedule;
288 current = current.nextSibling();
289 qDebug() << "marking as arrival";
290 if (current.isNull())
294 emit layoutChanged();
297 void StationScheduleModel::onNetworkError()
299 qDebug()<< "Station Schedule Model got a Network Error";
300 m_error = tr("Network error");
304 void StationScheduleModel::fetch(const QString &name, const QString &code)
306 DataProvider *provider = DataProvider::instance();
308 if (!error().isEmpty())
310 m_departureSchedules.clear();
311 m_arrivalSchedules.clear();
312 provider->fetchStationSchedule(name, code);
317 int StationScheduleModel::rowCount(const QModelIndex &parent) const
320 if (m_scheduleType == DepartureSchedule) {
321 return m_departureSchedules.count();
323 return m_arrivalSchedules.count();
327 QVariant StationScheduleModel::data(const QModelIndex &index, int role) const
329 if (!index.isValid()) {
332 const QList<StationScheduleItem> &schedules =
333 (m_scheduleType == DepartureSchedule) ? m_departureSchedules : m_arrivalSchedules;
334 if (index.row() < 0 || index.row() >= schedules.count()) {
337 StationScheduleItem item = schedules[index.row()];
339 case Qt::DisplayRole:
341 return QVariant::fromValue(item.train());
342 case DepartureStationRole:
343 return QVariant::fromValue(item.departureStation());
344 case DepartureTimeRole:
345 return QVariant::fromValue(item.departureTime());
346 case ArrivalStationRole:
347 return QVariant::fromValue(item.arrivalStation());
348 case ArrivalTimeRole:
349 return QVariant::fromValue(item.arrivalTime());
351 return QVariant::fromValue(item.detailsUrl());
353 return QVariant::fromValue(item.delay());
355 return QVariant::fromValue(item.delayClass());
356 case ExpectedPlatformRole:
357 return QVariant::fromValue(item.expectedPlatform());
358 case ActualPlatformRole:
359 return QVariant::fromValue(item.actualPlatform());
361 return QVariant::fromValue(QString("Unknown role requested"));