Changes:
[ptas] / zouba / route_p.cpp
1 #include "route_p.h"
2 #include "location.h"
3
4 #include <QXmlStreamReader>
5 #include <QDebug>
6 #include <QList>
7
8 RoutePrivate::RoutePrivate( QObject *parent ) :
9     m_fromValid(false),
10     m_toValid(false),
11     m_fromLocation(0,0),
12     m_toLocation(0,0)
13 {
14   Q_UNUSED( parent )
15 }
16
17 RoutePrivate::~RoutePrivate()
18 {
19 }
20
21 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
22 {
23   QList<RouteData> retVal;
24   RouteData routeData;
25
26   QXmlStreamReader xml( reply );
27
28   bool haveLine = false;
29   bool haveTime = false;
30
31   bool inLine = false;
32   bool inStop = false;
33   while ( !xml.atEnd() ) {
34     xml.readNext();
35     if ( xml.isStartElement() && xml.name() == "LINE" ) {
36       QString lineCode( xml.attributes().value("code").toString() );
37
38       routeData.lineCode = parseJORECode( lineCode );
39       haveLine = true;
40
41       inLine = true;
42     } else
43     if ( inLine && xml.name() == "STOP" ) {
44       inStop = true;
45     } else
46     if ( inLine && inStop && xml.name() == "ARRIVAL" ) {
47       QString arrivalTime( xml.attributes().value("time").toString() );
48
49       routeData.arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
50       haveTime = true;
51
52       inLine = false;
53     } else
54     if ( xml.isEndElement() && xml.name() == "STOP" ) {
55       inStop = false;
56       haveTime = false;
57     } else
58     if ( xml.isEndElement() && xml.name() == "LINE" ) {
59       inLine = false;
60       haveLine = false;
61     }
62
63     if ( haveLine && haveTime ) {
64       retVal.append( routeData );
65
66       // only want first STOP per LINE
67       haveTime = false;
68       haveLine = false;
69     }
70   }
71
72   if ( xml.hasError() ) {
73     qDebug() << "xml error";
74   }
75
76   return retVal;
77 }
78
79 void RoutePrivate::setFromLocation( const Location &location )
80 {
81   m_fromLocation = location;
82   m_fromValid = true;
83 }
84
85 const Location &RoutePrivate::fromLocation()
86 {
87   return m_fromLocation;
88 }
89
90 void RoutePrivate::setToLocation( const Location &toLocation )
91 {
92   m_toLocation = toLocation;
93   m_toValid = true;
94 }
95
96 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
97 {
98   QString retVal;
99
100   QString areaTransportTypeCode( joreCode.mid(0,1) );
101   QString lineCode( joreCode.mid(1,3) );
102   QString letterVariant( joreCode.mid(4,1) );
103   QString letterNumberVariant( joreCode.mid(5,1) );
104   QString direction( joreCode.mid(6,1) );
105
106   lineCode.setNum( lineCode.toInt() );
107
108   retVal = lineCode;
109   
110   if ( letterVariant != " " ) {
111     retVal += letterVariant;
112   }
113
114   return retVal;
115 }
116
117 const Location &RoutePrivate::toLocation()
118 {
119   return m_toLocation;
120 }
121
122 bool RoutePrivate::fromValid()
123 {
124   return m_fromValid;
125 }
126
127 bool RoutePrivate::toValid()
128 {
129   return m_toValid;
130 }