Renamed the httpclient class.
[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( routeData.lineCode );
64   ui->TimeDisplay->setText( routeData.arrivalTime );
65 }
66
67 void Route::setFromLocation( Location fromLocation )
68 {
69   q->setFromLocation( fromLocation );
70 }
71
72 Location Route::fromLocation()
73 {
74   return q->fromLocation();
75 }
76
77 void Route::setToLocation( Location toLocation )
78 {
79   q->setToLocation( toLocation );
80 }
81
82 Location Route::toLocation()
83 {
84   return q->toLocation();
85 }
86