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