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 for ( int index=0; index<haveKeys.count(); ++index ) {
56 have[ haveKeys.at(index) ] = false;
59 for ( int index=0; index<inKeys.count(); ++index ) {
60 in[ inKeys.at(index) ] = false;
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 // reset the 'have' flags to false
76 QList<QString> haveKeys = have.uniqueKeys();
77 for ( int haveKeysIndex=0; haveKeysIndex<have.count(); ++haveKeysIndex ) {
78 QString thisKey = haveKeys[ haveKeysIndex ];
79 have[ thisKey ] = false;
80 //qDebug() << "have[" << thisKey << "] = false";
84 if ( xmlName == "WALK" ) {
85 legData.m_how = "WALK";
86 have[ "DEPARTURE" ] = false;
87 have[ "ARRIVAL" ] = false;
88 have[ "LENGTH" ] = false;
91 if ( xmlName == "LINE" ) {
92 legData.m_how = "LINE";
93 QString lineCode( xml.attributes().value("code").toString() );
94 legData.m_lineCode = parseJORECode( lineCode );
95 have[ "DEPARTURE" ] = false;
96 have[ "ARRIVAL" ] = false;
97 have[ "LENGTH" ] = false;
101 if ( xml.isEndElement() ) {
102 if ( inKeys.contains( xmlName ) ) {
103 in[ xmlName ] = false;
104 //qDebug() << "in[" << xmlName << "] = false";
107 if ( xmlName == "ROUTE" ) {
108 retVal.append( routeData );
112 if ( xmlName == "WALK" || xmlName == "LINE" ) {
113 routeData.m_legData.append( legData );
115 have[ "LENGTH" ] = false;
119 if ( !have[ "ARRIVAL" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && ( in[ "STOP" ] || in[ "POINT" ] ) && xml.isStartElement() && xmlName == "ARRIVAL" ) {
120 QString arrivalTime( xml.attributes().value("time").toString() );
121 legData.m_arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
123 // don't set have[ "ARRIVAL" ] since we want the last one of many STOPs
126 if ( !have[ "DEPARTURE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.isStartElement() && xmlName == "DEPARTURE" ) {
127 QString departureTime( xml.attributes().value("time").toString() );
128 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
130 have[ "DEPARTURE" ] = true;
133 if ( !have[ "DEPARTURE" ] && in[ "WALK" ] && ( in[ "POINT" ] || in[ "STOP" ] ) && xml.isStartElement() && xmlName == "DEPARTURE" ) {
134 QString departureTime( xml.attributes().value("time").toString() );
135 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
137 have[ "DEPARTURE" ] = true;
140 if ( !have[ "LENGTH" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && xml.isStartElement() && xmlName == "LENGTH" ) {
141 legData.m_tripTime = xml.attributes().value("time").toString();
142 legData.m_tripDistance = xml.attributes().value("dist").toString();
144 have[ "LENGTH" ] = true;
147 if ( !have[ "TRIP" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LENGTH" ) {
148 routeData.m_tripTime = xml.attributes().value("time").toString();
149 routeData.m_tripDistance = xml.attributes().value("dist").toString();
151 have[ "TRIP" ] = true;
154 if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LINE" ) {
155 QString lineCode( xml.attributes().value("code").toString() );
157 routeData.m_lineCode = parseJORECode( lineCode );
158 have[ "LINE" ] = true;
161 if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xmlName == "DEPARTURE" ) {
162 QString departureTime( xml.attributes().value("time").toString() );
164 routeData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
165 have[ "TIME" ] = true;
170 if ( xml.hasError() ) {
171 qDebug() << "xml error:" << xml.errorString();
174 if ( retVal.isEmpty() ) {
175 qDebug() << "no routes found";
181 void RoutePrivate::setFromLocation( Location *location )
183 m_fromLocation = location;
187 Location *RoutePrivate::fromLocation() const
189 return m_fromLocation;
192 void RoutePrivate::setToLocation( Location *toLocation )
194 m_toLocation = toLocation;
198 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
202 QString areaTransportTypeCode( joreCode.mid(0,1) );
203 QString lineCode( joreCode.mid(1,3) );
204 QString letterVariant( joreCode.mid(4,1) );
205 QString letterNumberVariant( joreCode.mid(5,1) );
206 QString direction( joreCode.mid(6,1) );
208 lineCode.setNum( lineCode.toInt() );
212 if ( letterVariant != " " ) {
213 retVal += letterVariant;
219 Location *RoutePrivate::toLocation() const
224 bool RoutePrivate::fromValid()
229 bool RoutePrivate::toValid()