2 #include "logic/location.h"
4 #include <QXmlStreamReader>
10 #include <QMaemo5InformationBox>
13 RoutePrivate::RoutePrivate( QObject *parent ) :
22 RoutePrivate::~RoutePrivate()
26 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
28 qDebug() << "parsing route";
30 QList<RouteData> retVal;
34 QXmlStreamReader xml( reply );
36 QHash<QString, bool> in;
37 QHash<QString, bool> have;
58 foreach( QString key, haveKeys ) {
62 foreach( QString key, inKeys ) {
66 while ( !xml.atEnd() ) {
69 QString xmlName = xml.name().toString();
71 if ( xml.isStartElement() ) {
72 if ( inKeys.contains( xmlName ) ) {
74 //qDebug() << "in[" << xmlName << "] = true";
77 if ( xmlName == "ROUTE" ) {
78 foreach( QString key, haveKeys ) {
83 if ( xmlName == "WALK" ) {
84 legData.m_how = "WALK";
85 have[ "DEPARTURE" ] = false;
86 have[ "ARRIVAL" ] = false;
87 have[ "LENGTH" ] = false;
90 if ( xmlName == "LINE" ) {
91 legData.m_how = "LINE";
92 QString lineCode( xml.attributes().value("code").toString() );
93 legData.m_lineCode = parseJORECode( lineCode );
94 have[ "DEPARTURE" ] = false;
95 have[ "ARRIVAL" ] = false;
96 have[ "LENGTH" ] = false;
100 if ( xml.isEndElement() ) {
101 if ( inKeys.contains( xmlName ) ) {
102 in[ xmlName ] = false;
103 //qDebug() << "in[" << xmlName << "] = false";
106 if ( xmlName == "ROUTE" ) {
107 retVal.append( routeData );
111 if ( xmlName == "WALK" || xmlName == "LINE" ) {
112 routeData.m_legData.append( legData );
114 have[ "LENGTH" ] = false;
118 if ( !have[ "ARRIVAL" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && ( in[ "STOP" ] || in[ "POINT" ] ) && xml.isStartElement() && xmlName == "ARRIVAL" ) {
119 QString arrivalTime( xml.attributes().value("time").toString() );
120 legData.m_arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
122 // don't set have[ "ARRIVAL" ] since we want the last one of many STOPs
125 if ( !have[ "DEPARTURE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.isStartElement() && xmlName == "DEPARTURE" ) {
126 QString departureTime( xml.attributes().value("time").toString() );
127 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
129 have[ "DEPARTURE" ] = true;
132 if ( !have[ "DEPARTURE" ] && in[ "WALK" ] && ( in[ "POINT" ] || in[ "STOP" ] ) && xml.isStartElement() && xmlName == "DEPARTURE" ) {
133 QString departureTime( xml.attributes().value("time").toString() );
134 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
136 have[ "DEPARTURE" ] = true;
139 if ( !have[ "LENGTH" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && xml.isStartElement() && xmlName == "LENGTH" ) {
140 legData.m_tripTime = xml.attributes().value("time").toString();
141 legData.m_tripDistance = xml.attributes().value("dist").toString();
143 have[ "LENGTH" ] = true;
146 if ( !have[ "TRIP" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LENGTH" ) {
147 routeData.m_tripTime = xml.attributes().value("time").toString();
148 routeData.m_tripDistance = xml.attributes().value("dist").toString();
150 have[ "TRIP" ] = true;
153 if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LINE" ) {
154 QString lineCode( xml.attributes().value("code").toString() );
156 routeData.m_lineCode = parseJORECode( lineCode );
157 have[ "LINE" ] = true;
160 if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xmlName == "DEPARTURE" ) {
161 QString departureTime( xml.attributes().value("time").toString() );
163 routeData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
164 have[ "TIME" ] = true;
169 if ( xml.hasError() ) {
170 qDebug() << "xml error:" << xml.errorString();
173 if ( retVal.isEmpty() ) {
174 qDebug() << "no routes found";
176 QMaemo5InformationBox::information( 0, "no routes found" );
183 void RoutePrivate::setFromLocation( Location *location )
185 m_fromLocation = location;
189 Location *RoutePrivate::fromLocation() const
191 return m_fromLocation;
194 void RoutePrivate::setToLocation( Location *toLocation )
196 m_toLocation = toLocation;
200 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
204 QString areaTransportTypeCode( joreCode.mid(0,1) );
205 QString lineCode( joreCode.mid(1,3) );
206 QString letterVariant( joreCode.mid(4,1) );
207 QString letterNumberVariant( joreCode.mid(5,1) );
208 QString direction( joreCode.mid(6,1) );
210 lineCode.setNum( lineCode.toInt() );
214 if ( letterVariant != " " ) {
215 retVal += letterVariant;
221 Location *RoutePrivate::toLocation() const
226 bool RoutePrivate::fromValid()
231 bool RoutePrivate::toValid()