Basics to get xml from ytv server and parse the xml.
[ptas] / zouba / qt / httpclient.cpp
diff --git a/zouba/qt/httpclient.cpp b/zouba/qt/httpclient.cpp
new file mode 100644 (file)
index 0000000..8a66c95
--- /dev/null
@@ -0,0 +1,97 @@
+#include "httpclient.h"
+
+#include "ui_zouba.h"
+
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QUrl>
+#include <QObject>
+#include <QDebug>
+#include <QStringList>
+#include <QString>
+#include <QXmlStreamReader>
+
+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";
+  }
+}