Completed server clients sending functions.
authorTiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
Thu, 11 Mar 2010 07:05:45 +0000 (09:05 +0200)
committerTiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
Thu, 11 Mar 2010 07:05:45 +0000 (09:05 +0200)
Added temporarily hardcoded category list appear on UI.

Client/carmainwindow.cpp
Client/carmainwindow.h
Client/xmlreader.cpp

index 807159f..8a8deaf 100644 (file)
@@ -50,7 +50,7 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
 
     ui->labelMeasureTabResult->hide();
 
-    this->setWindowTitle("Speed freak");
+    this->setWindowTitle("Speed Freak");
 
 }
 
@@ -227,16 +227,21 @@ void CarMainWindow::on_registratePushButton_clicked()
   */
 void CarMainWindow::on_buttonTopRefresh_clicked()
 {
-    //setCategoryCompoBox();
-    requestTopList();
+    requestCategories();
+    setCategoryCompoBox();
 }
 
 /**
   *This slot function is called when ever category combobox current index changed. Top-tab view.
   *@param QString category
+  *@todo Check where limitNr is taken.
   */
 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
 {
+    int limitNr = 5;                    //replace with real value?
+    QString limit = QString::number(limitNr);
+    category = "acceleration-0-100";    //replace with real value from category list/top window
+    requestTopList(category, limit);
     setListViewTopList(category);
 }
 
@@ -261,7 +266,6 @@ void CarMainWindow::on_setUserPushButton_clicked()
   *@brief Sends registration information to the server in xml format.
   *Reads user name, password and emaol address from resuldialogs internal variables.
   *@todo Replace msg box with better reaction to server`s responce.
-  *@todo Write error handling.
   */
 void CarMainWindow::registrate()
 {
@@ -272,6 +276,7 @@ void CarMainWindow::registrate()
     QUrl qurl("http://api.speedfreak-app.com/api/register");
     QNetworkRequest request(qurl);
     qDebug() << qurl.toString();
+    QNetworkReply *currentDownload;
 
     regbuffer->open(QBuffer::ReadWrite);
     xmlwriter->writeRegistering(regbuffer,
@@ -281,91 +286,106 @@ void CarMainWindow::registrate()
     //Tmp msgbox - later server responce
     QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
 
-    manager->post(request, ("data=" + regbuffer->data()));
+    currentDownload = manager->post(request, ("xml=" + regbuffer->data()));
     qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
-    //ackOfRegistration function gets called when HTTP request is completed
-    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfRegistration(QNetworkReply*)));
-    //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
+    connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+
     regbuffer->close();
 }
 
+
 /**
-  *@brief Sends result(s) to the server in xml format with authentication information in the header.
-  *@todo Write error handling.
+  *@brief Sends result(s) to the server in xml format.
+  *Send authentication information in the header.
+  *@todo Read category elsewhere.
   */
-void CarMainWindow::sendXml()
+void CarMainWindow::sendResultXml()
 {
-    qDebug() << "_sendXml";
+    qDebug() << "_sendResultXml";
 
     QBuffer *xmlbuffer = new QBuffer();
     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/api/update/" + category_name);
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
-    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+    QNetworkReply *currentDownload;
 
     xmlbuffer->open(QBuffer::ReadWrite);
     xmlwriter->writeResult(xmlbuffer);
     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
 
-    manager->post(request, ("data=" + xmlbuffer->data()));
-    //connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*)));
-    //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
+    QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
+    credentials = "Basic " + credentials.toAscii().toBase64();
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
-    //QNetworkReply *currentDownload;
-    //QString data("abcdefg");
-    //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()), SLOT(ackOfResult()));
+    currentDownload = manager->post(request, ("xml=" + xmlbuffer->data()));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
+    connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
 
     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 limitNr
+  *@brief Request the Top10List of certain category from the server.
+  *Send authentication information in the header.
+  *@param Category of results.
+  *@param Limit, the number of results.
   */
-void CarMainWindow::requestTopList()
+void CarMainWindow::requestTopList(QString category, QString limit)
 {
     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 limitNr = 5;
-    QString limit = QString::number(limitNr);
+    QUrl qurl(urlBase + category + "/" + limit);
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
+    QNetworkReply *currentDownload;
 
     QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
     credentials = "Basic " + credentials.toAscii().toBase64();
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+    currentDownload = manager->post(request, ("data=" ));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
+    connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+}
+
 
-    QUrl qurl(urlBase + category_name + "/" + limit);
+/**
+  *@brief Request categories list from the server.
+  *Send authentication information in the header.
+  */
+void CarMainWindow::requestCategories()
+{
+    qDebug() << "_requestCategories" ;
+
+    QUrl qurl("http://api.speedfreak-app.com/api/categories/");
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
+    QNetworkReply *currentDownload;
 
+    QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
+    credentials = "Basic " + credentials.toAscii().toBase64();
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-    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()));
+    currentDownload = manager->post(request, ("data=" ));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
+    connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
 }
 
+
 /**
   *@brief React to servers responce after result has been sent.
-  *@todo Implement function and write error handling.
+  *@todo Implement consequencies of reply.
   */
-void CarMainWindow::ackOfResult(QNetworkReply* reply)
+void CarMainWindow::ackOfResult()
 {
     qDebug() << "_ackOfResult";
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     qDebug() << reply->readAll();
     QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
@@ -377,13 +397,16 @@ void CarMainWindow::ackOfResult(QNetworkReply* reply)
     }
 }
 
+
 /**
   *@brief React to servers responce after registration has been sent.
-  *@todo Implement function and write error handling.
+  *@todo Implement consequencies of reply.
   */
-void CarMainWindow::ackOfRegistration(QNetworkReply* reply)
+void CarMainWindow::ackOfRegistration()
 {
     qDebug() << "_ackOfRegistration";
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     qDebug() << reply->readAll();
     QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
@@ -395,13 +418,35 @@ void CarMainWindow::ackOfRegistration(QNetworkReply* reply)
     }
 }
 
-void CarMainWindow::errorFromServer(QNetworkReply* reply)
+
+/**
+  *@brief React to servers responce after request for categories has been sent.
+  *@todo Implement reply`s feeding to categories list.
+  */
+void CarMainWindow::ackOfCategories()
 {
-    qDebug() << "_errorFromServer";
-    QNetworkReply::NetworkError errorcode;
+    qDebug() << "_ackOfCategories";
 
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    qDebug() << reply->readAll();
+    QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
     if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
+}
+
+/**
+  *@brief Reports errors, when server has sent error signal.
+  */
+void CarMainWindow::errorFromServer(QNetworkReply::NetworkError errorcode)
+{
+    qDebug() << "_errorFromServer";
+
+    if(errorcode != 0) {
         qDebug() << errorcode;
     }
     else {
@@ -409,12 +454,16 @@ void CarMainWindow::errorFromServer(QNetworkReply* reply)
     }
 }
 
+
 /**
-  *This slot function is called when the server has finished guery.
+  *@brief React to servers responce after request of TopList in certain category has been sent.
+  *@todo Implement routing reply`s contents to UI.
   */
-void CarMainWindow::networkResponse(QNetworkReply *reply)
+void CarMainWindow::ackOfToplist()
 {
-    qDebug() << "_networkResponse";
+    qDebug() << "_ackOfToplist";
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     xmlreader->xmlRead(reply);
     qDebug() << reply->readAll();
     QNetworkReply::NetworkError errorcode;
@@ -427,6 +476,7 @@ void CarMainWindow::networkResponse(QNetworkReply *reply)
     }
 }
 
+
 /**
   *@brief Just for development, for the real button is not shown until
   *measurin started and there are results.
@@ -434,7 +484,7 @@ void CarMainWindow::networkResponse(QNetworkReply *reply)
   */
 void CarMainWindow::on_manualStartButton_clicked()
 {
-    sendXml();
+
 }
 
 /**
@@ -572,7 +622,7 @@ void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
 
 void CarMainWindow::on_pushButtonSendResult_clicked()
 {
-    sendXml();
+    sendResultXml();
     ui->pushButtonSendResult->setEnabled(false);
 }
 
index a7c7857..77c3252 100644 (file)
@@ -81,8 +81,8 @@ private:
     Measures *measures;
 
 signals:
-void speedAchieved();
-void userNameChanged();
+    void speedAchieved();
+    void userNameChanged();
 
 private slots:
     void on_pushButtonSendResult_clicked();
@@ -92,7 +92,6 @@ private slots:
     void on_registratePushButton_clicked();
     void on_comboBoxTopCategory_activated(QString );
     //void on_pushButton_clicked();
-    void networkResponse(QNetworkReply*);
     void on_comboBoxTopCategory_currentIndexChanged(QString category); //Top-tab view
     void on_listViewStartTabAccelerationCategories_clicked(QModelIndex index); //Start-tab view
     void updateComboBoxStartTabUnits(QString unit);  //Start-tab view
@@ -100,12 +99,14 @@ private slots:
     void on_buttonTopRefresh_clicked(); //Top-tab view: button
     void on_autoStartButton_clicked();  //Start-tab view: button
     void registrate();
-    void sendXml();
-    void requestTopList();
-    void ackOfResult(QNetworkReply* reply);
-    void ackOfRegistration(QNetworkReply* reply);
-    void errorFromServer(QNetworkReply* reply);
-
+    void sendResultXml();
+    void requestTopList(QString category, QString limit);
+    void requestCategories();
+    void ackOfResult();
+    void ackOfRegistration();
+    void ackOfCategories();
+    void ackOfToplist();
+    void errorFromServer(QNetworkReply::NetworkError);
     void after_timeout();
     void updateUserName();
 };
index 1ac3918..4d6430b 100644 (file)
@@ -129,6 +129,10 @@ void XmlReader::xmlShow()
   */
 QStringList XmlReader::getTop10List()
 {
+    //During development is needed some values for categorylist
+    top10List.clear();
+    top10List << "acceleration-0-10" << "acceleration-0-60" << "acceleration-0-100";
+
     return top10List;
 }