Measure-tab's time and speed shows bigger.
[speedfreak] / Client / carmainwindow.cpp
index 47859e1..8a8deaf 100644 (file)
@@ -19,6 +19,7 @@
 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
 {
     ui->setupUi(this);
+    ui->tabWidget->setCurrentWidget(this->ui->StartTab);
     //result = new ResultDialog();
     //measure = new MeasureDialog();
     xmlreader = new XmlReader();
@@ -30,9 +31,7 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     myRegistration = new Registration(this);
     xmlwriter = new XmlWriter();
     manager = new QNetworkAccessManager(this);
-    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
     connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
-    connect(this,SIGNAL(sendresult()),this,SLOT(sendXml()));
 
     time = 0;
     speed = 0;
@@ -40,17 +39,19 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
 
     accelerometer = new Accelerometer();
     accelerometer->setSampleRate(100);
-    accelerometer->start();
 
     measures = new Measures();
     this->initializeMeasures();
 
-    timer->setInterval(1000);
+    timer->setInterval(300);
 
     connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
+    connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
 
     ui->labelMeasureTabResult->hide();
 
+    this->setWindowTitle("Speed Freak");
+
 }
 
 /**
@@ -106,6 +107,7 @@ void CarMainWindow::on_autoStartButton_clicked()
     //measure = NULL;
     //measure = new MeasureDialog();
    // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
+    accelerometer->start();
     timer->start();
     // Show measure dialog.
     //measure->show();
@@ -201,6 +203,7 @@ void CarMainWindow::openResultView()
     //result->saveMeasuresToArray(measure->measures);
     // Show result dialog.
     //result->show();
+    ui->pushButtonSendResult->setEnabled(true);
     QString timeInteger;
     timeInteger.setNum(this->measures->getTime40kmh());
     //time = "0 - 40 km/h: ";
@@ -212,34 +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?
-  */
-void CarMainWindow::on_pushButton_clicked()
-{
-     sendXml();
-}
-
-/**
-  *This slot function is called when login/logout button is clicked.
-  */
-void CarMainWindow::on_loginLogoutButton_clicked()
-{
-    //LoginWindow myLogin;
-
-    myLogin->show();
-    //ui->loginLogoutButton->setText("logout");
-}
-
-/**
   *This slot function is called when registrate button is clicked.
   */
 void CarMainWindow::on_registratePushButton_clicked()
@@ -252,15 +227,21 @@ void CarMainWindow::on_registratePushButton_clicked()
   */
 void CarMainWindow::on_buttonTopRefresh_clicked()
 {
+    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);
 }
 
@@ -279,16 +260,12 @@ void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
 void CarMainWindow::on_setUserPushButton_clicked()
 {
     myLogin->show();
-
-    ui->userNameLabel->setText( "User: " + myLogin->getUserName());
-    ui->setUserPushButton->setText( "Change User");
 }
 
 /**
   *@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()
 {
@@ -296,16 +273,12 @@ 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();
+    QNetworkReply *currentDownload;
 
-    //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,60 +286,194 @@ 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()));
+    currentDownload = manager->post(request, ("xml=" + regbuffer->data()));
+    qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
-    //ackFromServer function gets called when HTTP request is completed
-    connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration()));
+    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
+
+    QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
 
+    xmlbuffer->open(QBuffer::ReadWrite);
+    xmlwriter->writeResult(xmlbuffer);
+    qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
+
     QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
     credentials = "Basic " + credentials.toAscii().toBase64();
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+    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)));
 
-    QUrl qurl("http//:api.speedfreak-app.com/update/acceleration-0-40");
+    xmlbuffer->close();
+}
+
+/**
+  *@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(QString category, QString limit)
+{
+    qDebug() << "_requestTopList" ;
+
+    QString urlBase = "http://api.speedfreak-app.com/api/results/";
+    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());
 
-    xmlwriter->writeResult(xmlbuffer);
+    currentDownload = manager->post(request, ("data=" ));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
+    connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+}
+
 
-    currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
-    //QString data("abcdefg");    //testing
-    //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data));   //testing
+/**
+  *@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;
 
-    //ackFromServer function gets called when HTTP request is completed
-    connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
+    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(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()
 {
-    qDebug() << "Server acknowledged posting of result";
+    qDebug() << "_ackOfResult";
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    qDebug() << reply->readAll();
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
 }
 
+
 /**
   *@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()
 {
-    qDebug() << "Server acknowledged registration";
+    qDebug() << "_ackOfRegistration";
+
+    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 React to servers responce after request for categories has been sent.
+  *@todo Implement reply`s feeding to categories list.
+  */
+void CarMainWindow::ackOfCategories()
+{
+    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 {
+        qDebug() << errorcode;
+    }
+}
+
+
+/**
+  *@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::ackOfToplist()
+{
+    qDebug() << "_ackOfToplist";
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    xmlreader->xmlRead(reply);
+    qDebug() << reply->readAll();
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
 }
 
 
@@ -375,10 +482,9 @@ void CarMainWindow::ackOfRegistration()
   *measurin started and there are results.
   *@todo Implement with real code and yet leave sendXml in the bottom in use.
   */
-
 void CarMainWindow::on_manualStartButton_clicked()
 {
-    sendXml();
+
 }
 
 /**
@@ -516,5 +622,25 @@ void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
 
 void CarMainWindow::on_pushButtonSendResult_clicked()
 {
-    emit sendresult();
+    sendResultXml();
+    ui->pushButtonSendResult->setEnabled(false);
+}
+
+void CarMainWindow::updateUserName()
+{
+    QString newUserName;
+
+    newUserName = myLogin->getUserName();
+    ui->userNameLabel->setText( "User: " + newUserName);
+
+    if (newUserName.length())
+    {
+       ui->setUserPushButton->setText( "Change User");
+       this->setWindowTitle("Speed freak - " + newUserName);
+    }
+    else
+    {
+        ui->setUserPushButton->setText( "Set User");
+        this->setWindowTitle("Speed freak");
+    }
 }