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