9e916201b1719447820d2f45e0f6f131d407292d
[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   qDebug() << __PRETTY_FUNCTION__;
23   RouteData retVal;
24
25   QXmlStreamReader xml( reply );
26
27   bool haveLine = false;
28   bool haveTime = false;
29
30   bool inLine = false;
31   bool inStop = false;
32   while ( !(haveLine && haveTime) && !xml.atEnd() ) {
33     xml.readNext();
34     if ( !haveLine && xml.isStartElement() && xml.name() == "LINE" ) {
35       QString lineCode( xml.attributes().value("code").toString() );
36       qDebug() << "lineCode" << lineCode;
37
38       retVal.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 ( !haveTime && inLine && inStop && xml.name() == "ARRIVAL" ) {
47       QString arrivalTime( xml.attributes().value("time").toString() );
48       qDebug() << "arrivalTime" << arrivalTime;
49
50       retVal.arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
51       haveTime = true;
52
53       inLine = false;
54     } else
55     if ( xml.isEndElement() && xml.name() == "STOP" ) {
56       inStop = false;
57     } else
58     if ( xml.isEndElement() && xml.name() == "LINE" ) {
59       inLine = false;
60     }
61   }
62
63   if ( xml.hasError() ) {
64     qDebug() << "xml error";
65   }
66
67   return retVal;
68 }
69
70 void RoutePrivate::setFromLocation( const Location &location )
71 {
72   m_fromLocation = location;
73   m_fromValid = true;
74 }
75
76 const Location &RoutePrivate::fromLocation()
77 {
78   return m_fromLocation;
79 }
80
81 void RoutePrivate::setToLocation( const Location &toLocation )
82 {
83   m_toLocation = toLocation;
84   m_toValid = true;
85 }
86
87 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
88 {
89     QString areaTransportTypeCode( joreCode.mid(0,1) );
90     QString lineCode( joreCode.mid(1,3) );
91     QString letterVariant( joreCode.mid(4,1) );
92     QString letterNumberVariant( joreCode.mid(5,1) );
93     QString direction( joreCode.mid(6,1) );
94
95     lineCode.setNum( lineCode.toInt() );
96     
97     return lineCode+letterVariant;
98 }
99
100 const Location &RoutePrivate::toLocation()
101 {
102   return m_toLocation;
103 }
104
105 bool RoutePrivate::fromValid()
106 {
107   return m_fromValid;
108 }
109
110 bool RoutePrivate::toValid()
111 {
112   return m_toValid;
113 }