Location selector and edit window finished
[ptas] / zouba / src / route.cpp
1 #include "route_p.h"
2 #include "route.h"
3
4 #include "routedata.h"
5 #include "location.h"
6
7 #include <QNetworkAccessManager>
8 #include <QNetworkReply>
9 #include <QUrl>
10 #include <QObject>
11 #include <QDebug>
12 #include <QStringList>
13 #include <QString>
14 #include <QXmlStreamReader>
15 #include <QWidget>
16
17 #include "ytv.h"
18
19 Route::Route() :
20   q( new RoutePrivate( this ) ),
21   manager( new QNetworkAccessManager(this) )
22 {
23   connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) );
24 }
25
26 Route::~Route()
27 {
28   delete manager;
29   manager = 0;
30 }
31
32 void Route::getRoute()
33 {
34   qDebug() << "getting route from Ytv";
35
36   QUrl fullUrl( Ytv::Url );
37
38   QStringList a;
39   a << q->fromLocation()->x() << q->fromLocation()->y();
40   QStringList b;
41   b << q->toLocation()->x() << q->toLocation()->y();
42
43   fullUrl.addQueryItem( "a", a.join(",") );
44   fullUrl.addQueryItem( "b", b.join(",") );
45   fullUrl.addQueryItem( "show", QString::number(Ytv::ShowFiveResults) );
46   fullUrl.addQueryItem( "walkspeed", QString::number(Ytv::WalkSpeedFast) );
47   fullUrl.addQueryItem( "optimize", QString::number(Ytv::OptimizeDefault) );
48   fullUrl.addQueryItem( "user", Ytv::Username );
49   fullUrl.addQueryItem( "pass", Ytv::Password );
50
51   manager->get( QNetworkRequest( fullUrl ) );
52   qDebug() << "getting url" << fullUrl.toEncoded();
53   qDebug() << "waiting for reply from Ytv";
54   emit( busy( true ) );
55 }
56
57 void Route::replyFinished( QNetworkReply * reply )
58 {
59   qDebug() << "have reply from Ytv";
60   QList<RouteData> routeData = q->parseReply( reply->readAll() );
61
62   emit( routeReady( routeData ) );
63   emit( busy( false ) );
64 }
65
66 void Route::setFromLocation( Location *location )
67 {
68   qDebug() << "setting new From location (" << location->label() << ")";
69   this->setLocation(location, true);
70 }
71
72 void Route::searchRoute()
73 {
74     if (q->fromValid() && q->toValid())
75     {
76         qDebug() << "From and To addresses are valid.";
77         getRoute();
78     }
79 }
80
81 Location *Route::fromLocation() const
82 {
83   return q->fromLocation();
84 }
85
86 void Route::setLocation(Location *location, bool from)
87 {
88   if (location != 0)
89   {
90     if (location->isValid())
91     {
92       qDebug() << "Location is valid";
93       if (from) q->setFromLocation( location );
94       else q->setToLocation(location);
95     } else {
96       qDebug() << "Location is not valid. Try again or fix address";
97       qDebug() << "Location = " << location;
98       //location->resolveAddress(location->address());
99     }
100   } else {
101     qDebug() << "ERROR:Null location pointer given.";
102   }
103 }
104
105 void Route::setToLocation( Location *location )
106 {
107   qDebug() << "setting new To location (" << location->label() << ")";
108   this->setLocation(location, false);
109 }
110
111 Location *Route::toLocation() const
112 {
113   return q->toLocation();
114 }