Handle error pages in the schedule model
authorLuciano Montanaro <mikelima@cirulla.net>
Sat, 4 Feb 2012 21:02:30 +0000 (22:02 +0100)
committerLuciano Montanaro <mikelima@cirulla.net>
Sat, 4 Feb 2012 21:02:30 +0000 (22:02 +0100)
application/stationschedulemodel.cpp

index 6f13b79..7343b30 100644 (file)
@@ -212,6 +212,20 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr
     page.mainFrame()->setContent(htmlReply, "text/html", baseUrl);
     QWebElement doc = page.mainFrame()->documentElement();
 
+    // Check if the page is reporting an error before parsing it
+    QWebElement errorElement = doc.findFirst("span.errore");
+    if (!errorElement.isNull()) {
+        m_departureSchedules.clear();
+        m_arrivalSchedules.clear();
+        QString errorText = errorElement.toPlainText().trimmed();
+        if (errorText == "localita' non trovata") {
+            setError(tr("Unknown station"));
+        } else {
+            setError(tr("Unknown error"));
+        }
+        qDebug() << "error:" << error();
+        return;
+    }
     // Find the first div
     QWebElement current = doc.findFirst("div");