b9783abf84d6daa675c0ad2aaa084031659e2b1a
[ptas] / zouba / src / route_p.cpp
1 #include "route_p.h"
2 #include "location.h"
3
4 #include <QXmlStreamReader>
5 #include <QDebug>
6 #include <QList>
7 #include <QFile>
8
9 RoutePrivate::RoutePrivate( QObject *parent ) :
10     m_fromValid(false),
11     m_toValid(false),
12     m_fromLocation(0),
13     m_toLocation(0)
14 {
15   Q_UNUSED( parent )
16 }
17
18 RoutePrivate::~RoutePrivate()
19 {
20 }
21
22 QList<RouteData> RoutePrivate::parseReply( const QByteArray &reply )
23 {
24   qDebug() << "parsing route";
25   QFile file( "/home/user/route.txt" );
26   if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
27     QTextStream out(&file);
28     out << reply;
29     file.close();
30   } else {
31     qDebug() << "Could not open /home/user/route.txt";
32   }
33
34
35   QList<RouteData> retVal;
36   RouteData routeData;
37
38   QXmlStreamReader xml( reply );
39
40   QHash<QString, bool> in;
41   QHash<QString, bool> have;
42
43   have[ "LINE" ] = false;
44   have[ "TIME" ] = false;
45
46   in[ "ROUTE" ] = false;
47   in[ "LINE" ]  = false;
48   in[ "STOP" ]  = false;
49
50   while ( !xml.atEnd() ) {
51     xml.readNext();
52     if ( xml.isStartElement() ) {
53         in[ xml.name().toString() ] = true;
54
55         if ( xml.name() == "ROUTE" ) {
56           have[ "TIME" ] = false;
57           have[ "LINE" ] = false;
58         }
59     }
60
61     if ( xml.isEndElement() ) {
62         in[ xml.name().toString() ] = false;
63     }
64
65     if ( !have[ "LINE" ] && in[ "ROUTE" ] && xml.isStartElement() && xml.name() == "LINE" ) {
66       QString lineCode( xml.attributes().value("code").toString() );
67
68       routeData.lineCode = parseJORECode( lineCode );
69       have[ "LINE" ] = true;
70
71       if ( have[ "LINE" ] && have[ "TIME" ] ) {
72         retVal.append( routeData );
73       }
74     }
75
76     if ( !have[ "TIME" ] && in[ "ROUTE" ] && in[ "LINE" ] && in[ "STOP" ] && xml.name() == "ARRIVAL" ) {
77       QString arrivalTime( xml.attributes().value("time").toString() );
78
79       routeData.arrivalTime = arrivalTime.rightJustified(4).insert(2,":");
80       have[ "TIME" ] = true;
81
82       if ( have[ "LINE" ] && have[ "TIME" ] ) {
83         retVal.append( routeData );
84       }
85     }
86
87   }
88
89   if ( xml.hasError() ) {
90     qDebug() << "xml error:" << xml.errorString();
91   }
92
93   if ( retVal.isEmpty() ) {
94     qDebug() << "no routes found";
95   }
96
97   return retVal;
98 }
99
100 void RoutePrivate::setFromLocation( Location *location )
101 {
102   m_fromLocation = location;
103   m_fromValid = true;
104 }
105
106 Location *RoutePrivate::fromLocation() const
107 {
108   return m_fromLocation;
109 }
110
111 void RoutePrivate::setToLocation( Location *toLocation )
112 {
113   m_toLocation = toLocation;
114   m_toValid = true;
115 }
116
117 QString RoutePrivate::parseJORECode( const QString &joreCode ) const
118 {
119   QString retVal;
120
121   QString areaTransportTypeCode( joreCode.mid(0,1) );
122   QString lineCode( joreCode.mid(1,3) );
123   QString letterVariant( joreCode.mid(4,1) );
124   QString letterNumberVariant( joreCode.mid(5,1) );
125   QString direction( joreCode.mid(6,1) );
126
127   lineCode.setNum( lineCode.toInt() );
128
129   retVal = lineCode;
130
131   if ( letterVariant != " " ) {
132     retVal += letterVariant;
133   }
134
135   return retVal;
136 }
137
138 Location *RoutePrivate::toLocation() const
139 {
140   return m_toLocation;
141 }
142
143 bool RoutePrivate::fromValid()
144 {
145   return m_fromValid;
146 }
147
148 bool RoutePrivate::toValid()
149 {
150   return m_toValid;
151 }