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