From 17639e6f1949d4e577803c655aefd299f7f43cd7 Mon Sep 17 00:00:00 2001 From: "tiina.kivilinna-korhola@windowslive.com" Date: Tue, 9 Mar 2010 20:45:44 +0200 Subject: [PATCH] Changed URLs. Added open/close to QBuffer buffers. --- Client/carmainwindow.cpp | 89 +++++++++++++++++++++++++--------------------- Client/registration.cpp | 12 +++---- Client/xmlreader.cpp | 3 +- Client/xmlreader.h | 3 +- Client/xmlwriter.cpp | 23 +++++------- Client/xmlwriter.h | 15 ++++++-- 6 files changed, 77 insertions(+), 68 deletions(-) diff --git a/Client/carmainwindow.cpp b/Client/carmainwindow.cpp index 5ec9688..9eb26ee 100644 --- a/Client/carmainwindow.cpp +++ b/Client/carmainwindow.cpp @@ -215,14 +215,6 @@ void CarMainWindow::openResultView() } /** - *This slot function is called when the server has finished guery. - */ -void CarMainWindow::networkResponse(QNetworkReply *reply) -{ - -} - -/** *This slot function is called when the user will to send data to server. *@todo Where is this callback connected? */ @@ -297,15 +289,11 @@ void CarMainWindow::registrate() qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail(); QBuffer *regbuffer = new QBuffer(); - QNetworkReply *currentDownload; - - QUrl qurl("http://api.speedfreak-app.com/register"); + QUrl qurl("http://api.speedfreak-app.com/api/register"); QNetworkRequest request(qurl); + qDebug() << qurl.toString(); - //write also to a file during development, : - xmlwriter->writeXml(this->myRegistration->getUserName(), - this->myRegistration->getPassword(), - this->myRegistration->getEmail()); + regbuffer->open(QBuffer::ReadWrite); xmlwriter->writeRegistering(regbuffer, this->myRegistration->getUserName(), this->myRegistration->getPassword(), @@ -313,16 +301,13 @@ void CarMainWindow::registrate() //Tmp msgbox - later server responce QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail()); - currentDownload = manager->post(request, ("data=" + regbuffer->data())); - - //ackFromServer function gets called when HTTP request is completed - connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration())); manager->post(request, ("data=" + regbuffer->data())); + qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data(); //ackOfRegistration function gets called when HTTP request is completed - //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfRegistration())); connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfRegistration(QNetworkReply*))); connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); + regbuffer->close(); } /** @@ -334,60 +319,64 @@ void CarMainWindow::sendXml() qDebug() << "_sendXml"; QBuffer *xmlbuffer = new QBuffer(); - QNetworkReply *currentDownload; - QString category_name = "acceleration-0-100"; //replace with real value from category list QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword(); credentials = "Basic " + credentials.toAscii().toBase64(); - QUrl qurl("http://api.speedfreak-app.com/update/category_name"); + QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name); + qDebug() << qurl.toString(); QNetworkRequest request(qurl); - request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + xmlbuffer->open(QBuffer::ReadWrite); xmlwriter->writeResult(xmlbuffer); + qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data(); - //currentDownload = manager->post(request, ("data=" + xmlbuffer->data())); manager->post(request, ("data=" + xmlbuffer->data())); - //QString data("abcdefg"); //testing - //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing + connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*))); + connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); + //QString data("abcdefg"); + //QNetworkReply *currentDownload; + //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing + //currentDownload = manager->post(request, ("data=" + xmlbuffer->data())); //ackOfResult function gets called when HTTP request is completed //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfResult())); - connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*))); - connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); + xmlbuffer->close(); } /** *@brief Sends request to the server for a top list with authentication information in the header. *@todo Write error handling. - *@todo Replace with real value from category list and limit + *@todo Replace with real value from category list and limitNr */ void CarMainWindow::requestTopList() { - qDebug() << "_registrate" ; + qDebug() << "_requestTopList" ; + QString urlBase = "http://api.speedfreak-app.com/api/results/"; QString category_name = "acceleration-0-100"; //replace with real value from category list/top window - int limit = 5; - //QNetworkReply *currentDownload; + int limitNr = 5; + QString limit = QString::number(limitNr); QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword(); credentials = "Basic " + credentials.toAscii().toBase64(); - QUrl qurl("http://api.speedfreak-app.com/results/category_name/limit"); + QUrl qurl(urlBase + category_name + "/" + limit); + qDebug() << qurl.toString(); QNetworkRequest request(qurl); request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); - - //currentDownload = manager->post(request, ("data=" )); manager->post(request, ("data=" )); + connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*))); + connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); + //QNetworkReply *currentDownload; + //currentDownload = manager->post(request, ("data=" )); //ackOfResult function gets called when HTTP request is completed //connect(currentDownload, SIGNAL(error()),SLOT(errorFromServer())); - connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*))); - connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); } /** @@ -397,13 +386,14 @@ void CarMainWindow::requestTopList() void CarMainWindow::ackOfResult(QNetworkReply* reply) { qDebug() << "_ackOfResult"; + qDebug() << reply->readAll(); QNetworkReply::NetworkError errorcode; errorcode = reply->error(); if(errorcode != 0) { qDebug() << errorcode << reply->errorString(); } else { - qDebug() << errorcode; + qDebug() << "errorcode=0"; } } @@ -418,10 +408,10 @@ void CarMainWindow::ackOfRegistration(QNetworkReply* reply) QNetworkReply::NetworkError errorcode; errorcode = reply->error(); if(errorcode != 0) { - qDebug() << errorcode << reply->errorString(); + qDebug() << "errorcode:" << errorcode << reply->errorString(); } else { - qDebug() << errorcode; + qDebug() << "errorcode=0"; } } @@ -439,7 +429,24 @@ void CarMainWindow::errorFromServer(QNetworkReply* reply) else { qDebug() << errorcode; } +} +/** + *This slot function is called when the server has finished guery. + */ +void CarMainWindow::networkResponse(QNetworkReply *reply) +{ + qDebug() << "_networkResponse"; + xmlreader->xmlRead(reply); + qDebug() << reply->readAll(); + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << errorcode << reply->errorString(); + } + else { + qDebug() << "errorcode=0"; + } } /** diff --git a/Client/registration.cpp b/Client/registration.cpp index b3ed87a..e767b01 100644 --- a/Client/registration.cpp +++ b/Client/registration.cpp @@ -17,6 +17,11 @@ Registration::Registration(QWidget *parent) : { ui->setupUi(this); this->setWindowTitle("Registration for Speed Freak server"); + + //Let`s not type it again + ui->newUsernameLineEdit->setText("user123"); + ui->newPasswordLineEdit->setText("salainen"); + ui->eMailLineEdit->setText("user123@emaili.fi"); } Registration::~Registration() @@ -38,13 +43,6 @@ void Registration::changeEvent(QEvent *e) void Registration::on_registratePushButton_clicked() { - //Let`s not type it again - ui->newUsernameLineEdit->setText("user123"); - ui->newPasswordLineEdit->setText("salainen"); - ui->eMailLineEdit->setText("user123@emaili.fi"); - - for(int i = 0; i < 3000; i++); - // Send username, password and email to SpeedFreak server this->username = ui->newUsernameLineEdit->text(); this->password = ui->newPasswordLineEdit->text(); diff --git a/Client/xmlreader.cpp b/Client/xmlreader.cpp index 772b28c..1ac3918 100644 --- a/Client/xmlreader.cpp +++ b/Client/xmlreader.cpp @@ -35,8 +35,7 @@ XmlReader::~XmlReader() /** *This function is used to parsing xml file. */ -//void XmlReader::xmlRead(QIODevice *device) -void XmlReader::xmlRead(QNetworkReply* device) +void XmlReader::xmlRead(QNetworkReply *device) { qDebug() << "_xmlRead"; diff --git a/Client/xmlreader.h b/Client/xmlreader.h index ec984ca..4cf31dd 100644 --- a/Client/xmlreader.h +++ b/Client/xmlreader.h @@ -40,8 +40,7 @@ private: QString value; public slots: - //void xmlRead(QIODevice* device); - void xmlRead(QNetworkReply* device); + void xmlRead(QNetworkReply *device); void xmlShow(); }; diff --git a/Client/xmlwriter.cpp b/Client/xmlwriter.cpp index 4d4c112..ff288d1 100644 --- a/Client/xmlwriter.cpp +++ b/Client/xmlwriter.cpp @@ -6,16 +6,6 @@ * license http://opensource.org/licenses/gpl-license.php GNU Public License */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "xmlwriter.h" @@ -26,7 +16,7 @@ */ XmlWriter::XmlWriter() { - + tmpvalue = 10; } /** @@ -36,6 +26,7 @@ XmlWriter::~XmlWriter() { } + /** *@brief Writes registration items into tags. *@param netbuf a buffer where xmlstreamwriter writes to. @@ -44,8 +35,9 @@ XmlWriter::~XmlWriter() *@param email. */ void XmlWriter::writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email) -//void XmlWriter::writeRegistering(QIODevice *netbuf, QString usr, QString psswd, QString email) { + qDebug() << "_writeRegistering"; + xmlwriter.setDevice(netbuf); xmlwriter.writeStartDocument(); @@ -74,13 +66,16 @@ void XmlWriter::writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QS *@todo Replace test value to finally used variables. */ void XmlWriter::writeResult(QBuffer *netbuf) -//void XmlWriter::writeResult(QIODevice *netbuf) { + qDebug() << "_writeResult"; + xmlwriter.setDevice(netbuf); xmlwriter.writeStartDocument(); xmlwriter.writeStartElement("result"); - xmlwriter.writeAttribute("value", QString::number(14)); + tmpvalue++; + qDebug() << tmpvalue; + xmlwriter.writeAttribute("value", QString::number(tmpvalue)); xmlwriter.writeAttribute("unit", "seconds"); xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString()); xmlwriter.writeEndElement(); diff --git a/Client/xmlwriter.h b/Client/xmlwriter.h index 9ed3b9f..539ccab 100644 --- a/Client/xmlwriter.h +++ b/Client/xmlwriter.h @@ -11,21 +11,32 @@ #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + class XmlWriter : public QObject { public: XmlWriter(); ~XmlWriter(); + int tmpvalue; private: QXmlStreamWriter xmlwriter; public slots: void writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email); - //void writeRegistering(QIODevice *netbuf, QString usr, QString psswd, QString email); void writeResult(QBuffer *netbuf); - //void writeResult(QIODevice *netbuf); bool writeXmlFile(QIODevice *device); void writeXml(QString usr, QString psswd, QString email); void writeItems(); -- 1.7.9.5