d2125da68da924f62331ecb9ff8b26997336cf4c
[ptas] / zouba / src / route_p.cpp
1 #include "route_p.h"
2 #include "location.h"
3
4 #include <QXmlStreamReader>
5 #include <QDebug>
6 #include <QList>
7 #include <QFile>
8 #include <QStringList>
9 #include <QMaemo5InformationBox>
10
11 RoutePrivate::RoutePrivate( QObject *parent ) :
12     m_fromValid(false),
13     m_toValid(false),
14     m_fromLocation(0),
15     m_toLocation(0)
16 {
17   Q_UNUSED( parent )
18 }
19
20 RoutePrivate::~RoutePrivate()
21 {
22 }
23
24 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
25 {
26   qDebug() << "parsing route";
27
28   QList<RouteData> retVal;
29   RouteData routeData;
30   LegData legData;
31
32   QXmlStreamReader xml( reply );
33
34   QHash<QString, bool> in;
35   QHash<QString, bool> have;
36
37   QStringList haveKeys;
38   QStringList inKeys;
39
40   haveKeys
41     << "LINE"
42     << "TIME"
43     << "TRIP"
44     << "DEPARTURE"
45     << "ARRIVAL"
46     ;
47
48   inKeys
49     << "ROUTE"
50     << "LINE"
51     << "STOP"
52     << "WALK"
53     << "POINT"
54     ;
55
56   foreach( QString key, haveKeys ) {
57     have[ key ] = false;
58   }
59
60   foreach( QString key, inKeys ) {
61     in[ key ] = false;
62   }
63
64   while ( !xml.atEnd() ) {
65     xml.readNext();
66
67     QString xmlName = xml.name().toString();
68
69     if ( xml.isStartElement() ) {
70       if ( inKeys.contains( xmlName ) ) {
71         in[ xmlName ] = true;
72         //qDebug() << "in[" << xmlName << "] = true";
73       }
74
75       if ( xmlName == "ROUTE" ) {
76         foreach( QString key, haveKeys ) {
77           have[ key ] = false;
78         }
79       }
80
81       if ( xmlName == "WALK" ) {
82         legData.m_how = "WALK";
83         have[ "DEPARTURE" ] = false;
84         have[ "ARRIVAL" ]   = false;
85         have[ "LENGTH" ]    = false;
86       }
87
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;
95       }
96     }
97
98     if ( xml.isEndElement() ) {
99       if ( inKeys.contains( xmlName ) ) {
100         in[ xmlName ] = false;
101         //qDebug() << "in[" << xmlName << "] = false";
102       }
103
104       if ( xmlName == "ROUTE" ) {
105         retVal.append( routeData );
106         routeData.clear();
107       }
108
109       if ( xmlName == "WALK" || xmlName == "LINE" ) {
110         routeData.m_legData.append( legData );
111         legData.clear();
112         have[ "LENGTH" ] = false;
113       }
114     }
115
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,":");
119
120       // don't set have[ "ARRIVAL" ] since we want the last one of many STOPs
121     }
122
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,":");
126
127       have[ "DEPARTURE" ] = true;
128     }
129
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,":");
133
134       have[ "DEPARTURE" ] = true;
135     }
136
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();
140
141       have[ "LENGTH" ] = true;
142     }
143
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();
147
148       have[ "TRIP" ] = true;
149     }
150
151     if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xmlName == "LINE" ) {
152       QString lineCode( xml.attributes().value("code").toString() );
153
154       routeData.m_lineCode = parseJORECode( lineCode );
155       have[ "LINE" ] = true;
156     }
157
158     if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xmlName == "DEPARTURE" ) {
159       QString departureTime( xml.attributes().value("time").toString() );
160
161       routeData.m_departureTime = departureTime.rightJustified(4).insert(2,":");
162       have[ "TIME" ] = true;
163     }
164
165   }
166
167   if ( xml.hasError() ) {
168     qDebug() << "xml error:" << xml.errorString();
169   }
170
171   if ( retVal.isEmpty() ) {
172     qDebug() << "no routes found";
173     QMaemo5InformationBox::information( 0, "no routes found" );
174   }
175
176   return retVal;
177 }
178
179 void RoutePrivate::setFromLocation( Location *location )
180 {
181   m_fromLocation = location;
182   m_fromValid = true;
183 }
184
185 Location *RoutePrivate::fromLocation() const
186 {
187   return m_fromLocation;
188 }
189
190 void RoutePrivate::setToLocation( Location *toLocation )
191 {
192   m_toLocation = toLocation;
193   m_toValid = true;
194 }
195
196 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
197 {
198   QString retVal;
199
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) );
205
206   lineCode.setNum( lineCode.toInt() );
207
208   retVal = lineCode;
209
210   if ( letterVariant != " " ) {
211     retVal += letterVariant;
212   }
213
214   return retVal;
215 }
216
217 Location *RoutePrivate::toLocation() const
218 {
219   return m_toLocation;
220 }
221
222 bool RoutePrivate::fromValid()
223 {
224   return m_fromValid;
225 }
226
227 bool RoutePrivate::toValid()
228 {
229   return m_toValid;
230 }