Changes: added routes with transfers (only displays first bus) and version 0.4
[ptas] / zouba / src / route_p.cpp
index 8cb2c7f..b9783ab 100644 (file)
@@ -4,6 +4,7 @@
 #include <QXmlStreamReader>
 #include <QDebug>
 #include <QList>
+#include <QFile>
 
 RoutePrivate::RoutePrivate( QObject *parent ) :
     m_fromValid(false),
@@ -21,54 +22,68 @@ RoutePrivate::~RoutePrivate()
 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
 {
   qDebug() << "parsing route";
+  QFile file( "/home/user/route.txt" );
+  if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+    QTextStream out(&file);
+    out << reply;
+    file.close();
+  } else {
+    qDebug() << "Could not open /home/user/route.txt";
+  }
+
 
   QList<RouteData> retVal;
   RouteData routeData;
 
   QXmlStreamReader xml( reply );
 
-  bool haveLine = false;
-  bool haveTime = false;
+  QHash<QString, bool> in;
+  QHash<QString, bool> have;
+
+  have[ "LINE" ] = false;
+  have[ "TIME" ] = false;
+
+  in[ "ROUTE" ] = false;
+  in[ "LINE" ]  = false;
+  in[ "STOP" ]  = false;
 
-  bool inLine = false;
-  bool inStop = false;
   while ( !xml.atEnd() ) {
     xml.readNext();
-    if ( xml.isStartElement() && xml.name() == "LINE" ) {
+    if ( xml.isStartElement() ) {
+        in[ xml.name().toString() ] = true;
+
+        if ( xml.name() == "ROUTE" ) {
+          have[ "TIME" ] = false;
+          have[ "LINE" ] = false;
+        }
+    }
+
+    if ( xml.isEndElement() ) {
+        in[ xml.name().toString() ] = false;
+    }
+
+    if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xml.name() == "LINE" ) {
       QString lineCode( xml.attributes().value("code").toString() );
 
       routeData.lineCode = parseJORECode( lineCode );
-      haveLine = true;
-
-      inLine = true;
-    } else
-    if ( inLine && xml.name() == "STOP" ) {
-      inStop = true;
-    } else
-    if ( inLine && inStop && xml.name() == "ARRIVAL" ) {
-      QString arrivalTime( xml.attributes().value("time").toString() );
+      have[ "LINE" ] = true;
 
-      routeData.arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
-      haveTime = true;
-
-      inLine = false;
-    } else
-    if ( xml.isEndElement() && xml.name() == "STOP" ) {
-      inStop = false;
-      haveTime = false;
-    } else
-    if ( xml.isEndElement() && xml.name() == "LINE" ) {
-      inLine = false;
-      haveLine = false;
+      if ( have[ "LINE" ] && have[ "TIME" ] ) {
+        retVal.append( routeData );
+      }
     }
 
-    if ( haveLine && haveTime ) {
-      retVal.append( routeData );
+    if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.name() == "ARRIVAL" ) {
+      QString arrivalTime( xml.attributes().value("time").toString() );
 
-      // only want first STOP per LINE
-      haveTime = false;
-      haveLine = false;
+      routeData.arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
+      have[ "TIME" ] = true;
+
+      if ( have[ "LINE" ] && have[ "TIME" ] ) {
+        retVal.append( routeData );
+      }
     }
+
   }
 
   if ( xml.hasError() ) {