+const QString &StationScheduleModel::code()
+{
+ return m_code;
+}
+
+void StationScheduleModel::setCode(const QString &code)
+{
+ if (code != m_code) {
+ m_code = code;
+ emit codeChanged();
+ }
+}
+
+const QString &StationScheduleModel::error()
+{
+ return m_error;
+}
+
+void StationScheduleModel::setError(const QString &error)
+{
+ if (error != m_error) {
+ m_error = error;
+ emit errorChanged();
+ }
+}
+
+StationScheduleModel::ScheduleType StationScheduleModel::type()
+{
+ return m_scheduleType;
+}
+
+void StationScheduleModel::setType(StationScheduleModel::ScheduleType type)
+{
+ if (type != m_scheduleType) {
+ emit layoutAboutToBeChanged();
+ beginResetModel();
+ m_scheduleType = type;
+ emit typeChanged();
+ endResetModel();
+ emit layoutChanged();
+ Settings *settings = Settings::instance();
+ settings->setShowArrivalsPreferred(m_scheduleType == ArrivalSchedule ? true : false);
+ }
+}
+
+static void parseDelayClass(const QWebElement &element, StationScheduleItem &item)
+{
+ if (!element.isNull()) {
+ QWebElement image = element.findFirst("img");
+ if (!image.isNull()) {
+ int delayClass = 42;
+ QString imageName = image.attribute("src");
+ if (!imageName.isEmpty()) {
+ QRegExp delayClassRegexp("pallinoRit([0-9])\\.png");
+ int pos = delayClassRegexp.indexIn(imageName);
+ qDebug() << "regexp matched at pos:" << pos << "match:" << delayClassRegexp.cap(0);
+ delayClass = (pos >= 0) ? delayClassRegexp.cap(1).toInt() : 0;
+ }
+ item.setDelayClass(delayClass);
+ } else {
+ qDebug() << "img not found";
+ }
+ } else {
+ qDebug() << "div.bloccotreno not found";
+ }
+}
+
+static void parseDetailsUrl(const QWebElement &element, StationScheduleItem &item)
+{
+ if (!element.isNull()) {
+ QWebElement link = element.findFirst("a");
+ QString url = link.attribute("href");
+ item.setDetailsUrl(url);
+ } else {
+ qDebug() << "link not found";
+ }
+}
+
+static void parseTrain(const QString &text, StationScheduleItem &item)
+{
+ QRegExp filter("^(Per|Da) (.*)\\n"
+ "Delle ore (.*)\n"
+ "Binario Previsto: (.*)\n"
+ "Binario Reale: (.*)\n"
+ "(.*)$");
+ int pos = filter.indexIn(text);
+ if (pos >= 0) {
+ if (filter.cap(1) == "Per") {
+ item.setDepartureStation(filter.cap(2));
+ item.setDepartureTime(filter.cap(3));
+ } else {
+ item.setArrivalStation(filter.cap(2));
+ item.setArrivalTime(filter.cap(3));
+ }
+ item.setDelay(filter.cap(6));
+ item.setExpectedPlatform(filter.cap(4));
+ item.setActualPlatform(filter.cap(5));
+ } else {
+ qDebug() << "could not parse" << text;
+ }
+}
+
+StationScheduleItem parseResult(const QWebElement &result)
+{
+ StationScheduleItem item;
+
+ QWebElement current = result.findFirst("h2");
+ if (!current.isNull()) {
+ item.setTrain(current.toPlainText());
+ }
+ parseDetailsUrl(result, item);
+ current = result.findFirst("div.bloccotreno");
+ parseDelayClass(current, item);
+ QString rawText = current.toPlainText();
+ parseTrain(rawText, item);
+
+ qDebug() << "train:" << item.train();
+ qDebug() << "delayClass:" << item.delayClass();
+ qDebug() << "detailsUrl:" << item.detailsUrl();
+ qDebug() << "departureStation:" << item.departureStation();
+ qDebug() << "departureTime:" << item.departureTime();
+ qDebug() << "arrivalStation:" << item.arrivalStation();
+ qDebug() << "arrivalTime:" << item.arrivalTime();
+ qDebug() << "expectedPlatform:" << item.expectedPlatform();
+ qDebug() << "actualPlatform:" << item.actualPlatform();
+ qDebug() << "delay:" << item.delay();
+ return item;
+}
+