Fix delay parsing
[quandoparte] / application / stationschedulemodel.cpp
index 7343b30..986d015 100644 (file)
@@ -24,10 +24,15 @@ Boston, MA 02110-1301, USA.
 #include "dataprovider.h"
 #include "settings.h"
 
+#include <QtGlobal>
 #include <QDebug>
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
+#include <QtWebKitWidgets>
+#else
 #include <QWebElement>
 #include <QWebFrame>
 #include <QWebPage>
+#endif
 
 StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) :
     QAbstractListModel(parent),
@@ -36,25 +41,15 @@ StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent)
 
 {
     DataProvider *provider = DataProvider::instance();
-    QHash<int, QByteArray> roles;
-    roles[TrainRole] = "train";
-    roles[DepartureStationRole] = "departureStation";
-    roles[DepartureTimeRole] = "departureTime";
-    roles[ArrivalStationRole] = "arrivalStation";
-    roles[ArrivalTimeRole] = "arrivalTime";
-    roles[DetailsUrlRole] = "detailsUrl";
-    roles[DelayRole] = "delay";
-    roles[DelayClassRole] = "delayClass";
-    roles[ExpectedPlatformRole] = "expectedPlatform";
-    roles[ActualPlatformRole] = "actualPlatform";
-    setRoleNames(roles);
-
     connect(provider, SIGNAL(stationScheduleReady(QByteArray,QUrl)),
             this, SLOT(parse(QByteArray,QUrl)));
     connect(provider, SIGNAL(error()),
             this, SLOT(onNetworkError()));
     Settings *settings = Settings::instance();
     m_scheduleType = settings->showArrivalsPreferred() ? ArrivalSchedule : DepartureSchedule;
+#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
+    setRoleNames(roleNames());
+#endif
 }
 
 const QString &StationScheduleModel::name()
@@ -96,6 +91,23 @@ void StationScheduleModel::setError(const QString &error)
     }
 }
 
+QHash<int, QByteArray> StationScheduleModel::roleNames() const
+{
+    QHash<int, QByteArray> roles;
+    roles[TrainRole] = "train";
+    roles[DepartureStationRole] = "departureStation";
+    roles[DepartureTimeRole] = "departureTime";
+    roles[ArrivalStationRole] = "arrivalStation";
+    roles[ArrivalTimeRole] = "arrivalTime";
+    roles[DetailsUrlRole] = "detailsUrl";
+    roles[DelayRole] = "delay";
+    roles[DelayClassRole] = "delayClass";
+    roles[ExpectedPlatformRole] = "expectedPlatform";
+    roles[ActualPlatformRole] = "actualPlatform";
+
+    return roles;
+}
+
 StationScheduleModel::ScheduleType StationScheduleModel::type()
 {
     return m_scheduleType;
@@ -164,7 +176,19 @@ static void parseTrain(const QString &text, StationScheduleItem &item)
             item.setArrivalStation(filter.cap(2));
             item.setArrivalTime(filter.cap(3));
         }
-        item.setDelay(filter.cap(6));
+        QString delayDescription = filter.cap(6);
+        if (delayDescription == "in orario") {
+            item.setDelay(QObject::tr("On time"));
+        } else {
+            QRegExp delayRegExp("ritardo ([0-9]+)(| minuti)");
+            int pos = delayRegExp.indexIn(delayDescription);
+            if (pos >= 0) {
+                item.setDelay(QString(QObject::tr("Delay: %1 minutes")).arg(delayRegExp.cap(1)));
+            } else {
+                // Does not match, let the user parse it
+                item.setDelay(delayDescription);
+            }
+        }
         item.setExpectedPlatform(filter.cap(4));
         item.setActualPlatform(filter.cap(5));
     } else {
@@ -229,7 +253,6 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr
     // Find the first div
     QWebElement current = doc.findFirst("div");
 
-    QStringList departures, arrivals;
     qDebug() << "skipping to the departures";
     // Skip to the first div of class corpocentrale, which contains the first
     // departure-related contents
@@ -244,7 +267,6 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr
     qDebug() << "marking departures";
     do {
         if (current.classes().contains("bloccorisultato")) {
-            departures << current.toPlainText();
             StationScheduleItem schedule = parseResult(current);
             if (schedule.isValid()) {
                 m_departureSchedules << schedule;
@@ -259,7 +281,6 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr
     // Mark everything as an arrival, until reaching the footer
     while (!current.classes().contains("footer")) {
         if (current.classes().contains("bloccorisultato")) {
-            arrivals << current.toPlainText();
             StationScheduleItem schedule = parseResult(current);
             if (schedule.isValid()) {
                 m_arrivalSchedules << schedule;