Added code to Route class to parse the JORE code.
[ptas] / zouba / qt / route.cpp
1 #include "route_p.h"
2 #include "route.h"
3
4 #include "routedata.h"
5 #include "location.h"
6
7 #include "ui_zouba.h"
8
9 #include <QNetworkAccessManager>
10 #include <QNetworkReply>
11 #include <QUrl>
12 #include <QObject>
13 #include <QDebug>
14 #include <QStringList>
15 #include <QString>
16 #include <QXmlStreamReader>
17
18 namespace {
19   QUrl ytv( "http://api.reittiopas.fi/public-ytv/fi/api/" );
20   QString username( "zouba" );
21   QString password( "caf9r3ee" );
22
23   QString homeKey( "taivaanvuohentie%207%2Chelsinki" );
24   QString workKey( "it%E4merenkatu%2011%2Chelsinki" );
25
26 }
27   
28 Route::Route( Ui::MainWindow *ui ) :
29   q( new RoutePrivate( this ) ),
30   manager( new QNetworkAccessManager(this) ),
31   ui( ui )
32 {
33   connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
34 }
35
36 Route::~Route()
37 {
38   delete manager;
39   manager = 0;
40 }
41
42 void Route::get()
43 {
44   QUrl fullUrl( ytv );
45
46   QStringList a;
47   a << q->fromLocation().x << q->fromLocation().y;
48   QStringList b;
49   b << q->toLocation().x << q->toLocation().y;
50
51   fullUrl.addQueryItem( "a", a.join(",") );
52   fullUrl.addQueryItem( "b", b.join(",") );
53   fullUrl.addQueryItem( "user", username );
54   fullUrl.addQueryItem( "pass", password );
55
56   manager->get( QNetworkRequest( fullUrl ) );
57 }
58
59 void Route::replyFinished( QNetworkReply * reply )
60 {
61   RouteData routeData = q->parseReply( reply->readAll() );
62
63   ui->BusNoDisplay->setText( parseJOREcode( routeData.lineCode ) );
64   ui->TimeDisplay->setText( routeData.arrivalTime );
65 }
66
67 QString Route::parseJOREcode( const QString &joreCode ) const
68 {
69     QString areaTransportTypeCode( joreCode.mid(0,1) );
70     QString lineCode( joreCode.mid(1,4) );
71     QString letterVariant( joreCode.mid(5,1) );
72     QString letterNumberVariant( joreCode.mid(6,1) );
73     QString direction( joreCode.mid(7,1) );
74     
75     return lineCode+letterVariant;
76 }
77
78 void Route::setFromLocation( Location fromLocation )
79 {
80   q->setFromLocation( fromLocation );
81 }
82
83 Location Route::fromLocation()
84 {
85   return q->fromLocation();
86 }
87
88 void Route::setToLocation( Location toLocation )
89 {
90   q->setToLocation( toLocation );
91 }
92
93 Location Route::toLocation()
94 {
95   return q->toLocation();
96 }
97