Changed URLs. Added open/close to QBuffer buffers.
authortiina.kivilinna-korhola@windowslive.com <tkk@tkk-laptop.(none)>
Tue, 9 Mar 2010 18:45:44 +0000 (20:45 +0200)
committertiina.kivilinna-korhola@windowslive.com <tkk@tkk-laptop.(none)>
Tue, 9 Mar 2010 18:45:44 +0000 (20:45 +0200)
Client/carmainwindow.cpp
Client/registration.cpp
Client/xmlreader.cpp
Client/xmlreader.h
Client/xmlwriter.cpp
Client/xmlwriter.h

index 5ec9688..9eb26ee 100644 (file)
@@ -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?
   */
   *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();
     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);
     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(),
     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());
 
     //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()));
     manager->post(request, ("data=" + regbuffer->data()));
+    qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
     //ackOfRegistration function gets called when HTTP request is completed
 
     //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*)));
     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();
     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();
 
     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);
     QNetworkRequest request(qurl);
-
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
+    xmlbuffer->open(QBuffer::ReadWrite);
     xmlwriter->writeResult(xmlbuffer);
     xmlwriter->writeResult(xmlbuffer);
+    qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
 
 
-    //currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
     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()));
     //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.
 }
 
 /**
   *@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()
 {
   */
 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
     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();
 
 
     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());
     QNetworkRequest request(qurl);
 
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
-    //currentDownload = manager->post(request, ("data=" ));
     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()));
     //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";
 void CarMainWindow::ackOfResult(QNetworkReply* reply)
 {
     qDebug() << "_ackOfResult";
+    qDebug() << reply->readAll();
     QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
     if(errorcode != 0) {
         qDebug() << errorcode << reply->errorString();
     }
     else {
     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) {
     QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
     if(errorcode != 0) {
-        qDebug() << errorcode << reply->errorString();
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
     }
     else {
     }
     else {
-        qDebug() << errorcode;
+        qDebug() << "errorcode=0";
     }
 }
 
     }
 }
 
@@ -439,7 +429,24 @@ void CarMainWindow::errorFromServer(QNetworkReply* reply)
     else {
         qDebug() << errorcode;
     }
     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";
+    }
 }
 
 /**
 }
 
 /**
index b3ed87a..e767b01 100644 (file)
@@ -17,6 +17,11 @@ Registration::Registration(QWidget *parent) :
 {
     ui->setupUi(this);
     this->setWindowTitle("Registration for Speed Freak server");
 {
     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()
 }
 
 Registration::~Registration()
@@ -38,13 +43,6 @@ void Registration::changeEvent(QEvent *e)
 
 void Registration::on_registratePushButton_clicked()
 {
 
 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();
     // Send username, password and email to SpeedFreak server
     this->username = ui->newUsernameLineEdit->text();
     this->password = ui->newPasswordLineEdit->text();
index 772b28c..1ac3918 100644 (file)
@@ -35,8 +35,7 @@ XmlReader::~XmlReader()
 /**
   *This function is used to parsing xml file.
   */
 /**
   *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";
 
 {
     qDebug() << "_xmlRead";
 
index ec984ca..4cf31dd 100644 (file)
@@ -40,8 +40,7 @@ private:
     QString value;
 
 public slots:
     QString value;
 
 public slots:
-    //void xmlRead(QIODevice* device);
-    void xmlRead(QNetworkReply* device);
+    void xmlRead(QNetworkReply *device);
     void xmlShow();
 };
 
     void xmlShow();
 };
 
index 4d4c112..ff288d1 100644 (file)
@@ -6,16 +6,6 @@
  * license     http://opensource.org/licenses/gpl-license.php GNU Public License
  */
 
  * license     http://opensource.org/licenses/gpl-license.php GNU Public License
  */
 
-#include <QtGui>
-#include <QNetworkRequest>
-#include <QIODevice>
-#include <QFile>
-#include <QMessageBox>
-#include <QDebug>
-#include <QDateTime>
-#include <QDate>
-#include <QTime>
-#include <QApplication>
 #include "xmlwriter.h"
 
 
 #include "xmlwriter.h"
 
 
@@ -26,7 +16,7 @@
   */
 XmlWriter::XmlWriter()
 {
   */
 XmlWriter::XmlWriter()
 {
-
+    tmpvalue = 10;
 }
 
 /**
 }
 
 /**
@@ -36,6 +26,7 @@ XmlWriter::~XmlWriter()
 {
 
 }
 {
 
 }
+
 /**
   *@brief Writes registration items into tags.
   *@param netbuf a buffer where xmlstreamwriter writes to.
 /**
   *@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)
   *@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();
     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)
   *@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.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();
     xmlwriter.writeAttribute("unit", "seconds");
     xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
     xmlwriter.writeEndElement();
index 9ed3b9f..539ccab 100644 (file)
 
 #include <QXmlStreamWriter>
 #include <QBuffer>
 
 #include <QXmlStreamWriter>
 #include <QBuffer>
+#include <QtGui>
+#include <QNetworkRequest>
+#include <QIODevice>
+#include <QFile>
+#include <QMessageBox>
+#include <QDebug>
+#include <QDateTime>
+#include <QDate>
+#include <QTime>
+#include <QByteArray>
+#include <QApplication>
+
 
 class XmlWriter : public QObject
 {
 public:
     XmlWriter();
     ~XmlWriter();
 
 class XmlWriter : public QObject
 {
 public:
     XmlWriter();
     ~XmlWriter();
+    int tmpvalue;
 
 private:
     QXmlStreamWriter xmlwriter;
 
 public slots:
     void writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email);
 
 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(QBuffer *netbuf);
-    //void writeResult(QIODevice *netbuf);
     bool writeXmlFile(QIODevice *device);
     void writeXml(QString usr, QString psswd, QString email);
     void writeItems();
     bool writeXmlFile(QIODevice *device);
     void writeXml(QString usr, QString psswd, QString email);
     void writeItems();