4 #include <QXmlStreamReader>
10 RoutePrivate::RoutePrivate( QObject *parent ) :
19 RoutePrivate::~RoutePrivate()
23 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
25 qDebug() << "parsing route";
27 QList<RouteData> retVal;
31 QXmlStreamReader xml( reply );
33 QHash<QString, bool> in;
34 QHash<QString, bool> have;
55 foreach( QString key, haveKeys ) {
59 foreach( QString key, inKeys ) {
63 while ( !xml.atEnd() ) {
66 QString xmlName = xml.name().toString();
68 if ( xml.isStartElement() ) {
69 if ( inKeys.contains( xmlName ) ) {
71 //qDebug() << "in[" << xmlName << "] = true";
74 if ( xmlName == "ROUTE" ) {
75 foreach( QString key, haveKeys ) {
80 if ( xmlName == "WALK" ) {
81 legData.m_how = "WALK";
82 have[ "DEPARTURE" ] = false;
83 have[ "ARRIVAL" ] = false;
84 have[ "LENGTH" ] = false;
87 if ( xmlName == "LINE" ) {
88 legData.m_how = "LINE";
89 QString lineCode( xml.attributes().value("code").toString() );
90 legData.m_lineCode = parseJORECode( lineCode );
91 have[ "DEPARTURE" ] = false;
92 have[ "ARRIVAL" ] = false;
93 have[ "LENGTH" ] = false;
97 if ( xml.isEndElement() ) {
98 if ( inKeys.contains( xmlName ) ) {
99 in[ xmlName ] = false;
100 //qDebug() << "in[" << xmlName << "] = false";
103 if ( xmlName == "ROUTE" ) {
104 retVal.append( routeData );
108 if ( xmlName == "WALK" || xmlName == "LINE" ) {
109 routeData.m_legData.append( legData );
111 have[ "LENGTH" ] = false;
115 if ( !have[ "ARRIVAL" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && ( in[ "STOP" ] || in[ "POINT" ] ) && xml.isStartElement() && xmlName == "ARRIVAL" ) {
116 QString arrivalTime( xml.attributes().value("time").toString() );
117 legData.m_arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
119 // don't set have[ "ARRIVAL" ] since we want the last one of many STOPs
122 if ( !have[ "DEPARTURE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.isStartElement() && xmlName == "DEPARTURE" ) {
123 QString departureTime( xml.attributes().value("time").toString() );
124 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
126 have[ "DEPARTURE" ] = true;
129 if ( !have[ "DEPARTURE" ] && in[ "WALK" ] && ( in[ "POINT" ] || in[ "STOP" ] ) && xml.isStartElement() && xmlName == "DEPARTURE" ) {
130 QString departureTime( xml.attributes().value("time").toString() );
131 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
133 have[ "DEPARTURE" ] = true;
136 if ( !have[ "LENGTH" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && xml.isStartElement() && xmlName == "LENGTH" ) {
137 legData.m_tripTime = xml.attributes().value("time").toString();
138 legData.m_tripDistance = xml.attributes().value("dist").toString();
140 have[ "LENGTH" ] = true;
143 if ( !have[ "TRIP" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LENGTH" ) {
144 routeData.m_tripTime = xml.attributes().value("time").toString();
145 routeData.m_tripDistance = xml.attributes().value("dist").toString();
147 have[ "TRIP" ] = true;
150 if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LINE" ) {
151 QString lineCode( xml.attributes().value("code").toString() );
153 routeData.m_lineCode = parseJORECode( lineCode );
154 have[ "LINE" ] = true;
157 if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xmlName == "DEPARTURE" ) {
158 QString departureTime( xml.attributes().value("time").toString() );
160 routeData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
161 have[ "TIME" ] = true;
166 if ( xml.hasError() ) {
167 qDebug() << "xml error:" << xml.errorString();
170 if ( retVal.isEmpty() ) {
171 qDebug() << "no routes found";
177 void RoutePrivate::setFromLocation( Location *location )
179 m_fromLocation = location;
183 Location *RoutePrivate::fromLocation() const
185 return m_fromLocation;
188 void RoutePrivate::setToLocation( Location *toLocation )
190 m_toLocation = toLocation;
194 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
198 QString areaTransportTypeCode( joreCode.mid(0,1) );
199 QString lineCode( joreCode.mid(1,3) );
200 QString letterVariant( joreCode.mid(4,1) );
201 QString letterNumberVariant( joreCode.mid(5,1) );
202 QString direction( joreCode.mid(6,1) );
204 lineCode.setNum( lineCode.toInt() );
208 if ( letterVariant != " " ) {
209 retVal += letterVariant;
215 Location *RoutePrivate::toLocation() const
220 bool RoutePrivate::fromValid()
225 bool RoutePrivate::toValid()