4 #include <QXmlStreamReader>
9 #include <QMaemo5InformationBox>
11 RoutePrivate::RoutePrivate( QObject *parent ) :
20 RoutePrivate::~RoutePrivate()
24 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
26 qDebug() << "parsing route";
28 QList<RouteData> retVal;
32 QXmlStreamReader xml( reply );
34 QHash<QString, bool> in;
35 QHash<QString, bool> have;
56 foreach( QString key, haveKeys ) {
60 foreach( QString key, inKeys ) {
64 while ( !xml.atEnd() ) {
67 QString xmlName = xml.name().toString();
69 if ( xml.isStartElement() ) {
70 if ( inKeys.contains( xmlName ) ) {
72 //qDebug() << "in[" << xmlName << "] = true";
75 if ( xmlName == "ROUTE" ) {
76 foreach( QString key, haveKeys ) {
81 if ( xmlName == "WALK" ) {
82 legData.m_how = "WALK";
83 have[ "DEPARTURE" ] = false;
84 have[ "ARRIVAL" ] = false;
85 have[ "LENGTH" ] = false;
88 if ( xmlName == "LINE" ) {
89 legData.m_how = "LINE";
90 QString lineCode( xml.attributes().value("code").toString() );
91 legData.m_lineCode = parseJORECode( lineCode );
92 have[ "DEPARTURE" ] = false;
93 have[ "ARRIVAL" ] = false;
94 have[ "LENGTH" ] = false;
98 if ( xml.isEndElement() ) {
99 if ( inKeys.contains( xmlName ) ) {
100 in[ xmlName ] = false;
101 //qDebug() << "in[" << xmlName << "] = false";
104 if ( xmlName == "ROUTE" ) {
105 retVal.append( routeData );
109 if ( xmlName == "WALK" || xmlName == "LINE" ) {
110 routeData.m_legData.append( legData );
112 have[ "LENGTH" ] = false;
116 if ( !have[ "ARRIVAL" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && ( in[ "STOP" ] || in[ "POINT" ] ) && xml.isStartElement() && xmlName == "ARRIVAL" ) {
117 QString arrivalTime( xml.attributes().value("time").toString() );
118 legData.m_arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
120 // don't set have[ "ARRIVAL" ] since we want the last one of many STOPs
123 if ( !have[ "DEPARTURE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.isStartElement() && xmlName == "DEPARTURE" ) {
124 QString departureTime( xml.attributes().value("time").toString() );
125 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
127 have[ "DEPARTURE" ] = true;
130 if ( !have[ "DEPARTURE" ] && in[ "WALK" ] && ( in[ "POINT" ] || in[ "STOP" ] ) && xml.isStartElement() && xmlName == "DEPARTURE" ) {
131 QString departureTime( xml.attributes().value("time").toString() );
132 legData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
134 have[ "DEPARTURE" ] = true;
137 if ( !have[ "LENGTH" ] && ( in[ "WALK" ] || in[ "LINE" ] ) && xml.isStartElement() && xmlName == "LENGTH" ) {
138 legData.m_tripTime = xml.attributes().value("time").toString();
139 legData.m_tripDistance = xml.attributes().value("dist").toString();
141 have[ "LENGTH" ] = true;
144 if ( !have[ "TRIP" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LENGTH" ) {
145 routeData.m_tripTime = xml.attributes().value("time").toString();
146 routeData.m_tripDistance = xml.attributes().value("dist").toString();
148 have[ "TRIP" ] = true;
151 if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LINE" ) {
152 QString lineCode( xml.attributes().value("code").toString() );
154 routeData.m_lineCode = parseJORECode( lineCode );
155 have[ "LINE" ] = true;
158 if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xmlName == "DEPARTURE" ) {
159 QString departureTime( xml.attributes().value("time").toString() );
161 routeData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
162 have[ "TIME" ] = true;
167 if ( xml.hasError() ) {
168 qDebug() << "xml error:" << xml.errorString();
171 if ( retVal.isEmpty() ) {
172 qDebug() << "no routes found";
173 QMaemo5InformationBox::information( 0, "no routes found" );
179 void RoutePrivate::setFromLocation( Location *location )
181 m_fromLocation = location;
185 Location *RoutePrivate::fromLocation() const
187 return m_fromLocation;
190 void RoutePrivate::setToLocation( Location *toLocation )
192 m_toLocation = toLocation;
196 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
200 QString areaTransportTypeCode( joreCode.mid(0,1) );
201 QString lineCode( joreCode.mid(1,3) );
202 QString letterVariant( joreCode.mid(4,1) );
203 QString letterNumberVariant( joreCode.mid(5,1) );
204 QString direction( joreCode.mid(6,1) );
206 lineCode.setNum( lineCode.toInt() );
210 if ( letterVariant != " " ) {
211 retVal += letterVariant;
217 Location *RoutePrivate::toLocation() const
222 bool RoutePrivate::fromValid()
227 bool RoutePrivate::toValid()