X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=zouba%2Fqt%2Fhttpclient.cpp;fp=zouba%2Fqt%2Fhttpclient.cpp;h=8a66c9557062cdadeb6e78245d4188bfb440da96;hb=ad4b3be977845e6435d55f41a17b1622661a3345;hp=0000000000000000000000000000000000000000;hpb=416f018d3eadc98e82817b2a36e38ac8b1e451e6;p=ptas diff --git a/zouba/qt/httpclient.cpp b/zouba/qt/httpclient.cpp new file mode 100644 index 0000000..8a66c95 --- /dev/null +++ b/zouba/qt/httpclient.cpp @@ -0,0 +1,97 @@ +#include "httpclient.h" + +#include "ui_zouba.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + QUrl ytv( "http://api.reittiopas.fi/public-ytv/fi/api/" ); + QString username( "zouba" ); + QString password( "caf9r3ee" ); + + QString homeKey( "taivaanvuohentie%207%2Chelsinki" ); + QString workKey( "it%E4merenkatu%2011%2Chelsinki" ); + + QString workX( "2551042" ); + QString workY( "6672829" ); + QString homeX( "2549183" ); + QString homeY( "6672570" ); +} + +HttpClient::HttpClient( Ui::MainWindow *ui ) : + manager( new QNetworkAccessManager(this) ), + ui( ui ) +{ + connect( manager, SIGNAL( finished(QNetworkReply*) ), this, SLOT( replyFinished(QNetworkReply*) ) ); +} + +HttpClient::~HttpClient() +{ + delete manager; + manager = 0; +} + +void HttpClient::get() +{ + QUrl fullUrl( ytv ); + + QStringList a; + a << workX << workY; + QStringList b; + b << homeX << homeY; + + fullUrl.addQueryItem( "a", a.join(",") ); + fullUrl.addQueryItem( "b", b.join(",") ); + fullUrl.addQueryItem( "user", username ); + fullUrl.addQueryItem( "pass", password ); + + manager->get( QNetworkRequest( fullUrl ) ); +} + +void HttpClient::replyFinished( QNetworkReply * reply ) +{ + QXmlStreamReader xml( reply->readAll() ); + + bool inLine = false; + bool inStop = false; + while ( !xml.atEnd() ) { + xml.readNext(); + //qDebug() << xml.name(); + if ( xml.isStartElement() && xml.name() == "LINE" ) { + QString lineCode( xml.attributes().value("code").toString() ); + + qDebug() << "line code" << lineCode; + ui->BusNoDisplay->setText( lineCode ); + + inLine = true; + } else + if ( inLine && xml.name() == "STOP" ) { + inStop = true; + } else + if ( inLine && inStop && xml.name() == "ARRIVAL" ) { + QString arrivalTime( xml.attributes().value("time").toString() ); + + qDebug() << "arrival time" << arrivalTime; + ui->TimeDisplay->setText( arrivalTime ); + + inLine = false; + } else + if ( xml.isEndElement() && xml.name() == "STOP" ) { + inStop = false; + } else + if ( xml.isEndElement() && xml.name() == "LINE" ) { + inLine = false; + } + } + + if ( xml.hasError() ) { + qDebug() << "xml error"; + } +}