Merge branch 'changes/UI'
[speedfreak] / Client / carmainwindow.cpp
index 0371be1..807159f 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;
@@ -47,9 +46,12 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     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");
+
 }
 
 /**
@@ -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,7 +227,8 @@ void CarMainWindow::on_registratePushButton_clicked()
   */
 void CarMainWindow::on_buttonTopRefresh_clicked()
 {
-    setCategoryCompoBox();
+    //setCategoryCompoBox();
+    requestTopList();
 }
 
 /**
@@ -279,9 +255,6 @@ 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");
 }
 
 /**
@@ -296,16 +269,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,10 +281,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()));
+    manager->post(request, ("data=" + regbuffer->data()));
+    qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
-    //ackFromServer function gets called when HTTP request is completed
-    connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration()));
+    //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*)));
+    regbuffer->close();
 }
 
 /**
@@ -328,54 +299,139 @@ 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/acceleration-0-40");
+    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();
+
+    manager->post(request, ("data=" + xmlbuffer->data()));
+    //connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*)));
+    //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
 
-    currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
-    //QString data("abcdefg");    //testing
+    //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()));
+
+    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
+  */
+void CarMainWindow::requestTopList()
+{
+    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);
 
-    //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();
+
+    QUrl qurl(urlBase + category_name + "/" + limit);
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
 
+    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()));
 }
 
 /**
   *@brief React to servers responce after result has been sent.
   *@todo Implement function and write error handling.
   */
-void CarMainWindow::ackOfResult()
+void CarMainWindow::ackOfResult(QNetworkReply* reply)
 {
-    qDebug() << "Server acknowledged posting of result";
+    qDebug() << "_ackOfResult";
+    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.
   */
+void CarMainWindow::ackOfRegistration(QNetworkReply* reply)
+{
+    qDebug() << "_ackOfRegistration";
+    qDebug() << reply->readAll();
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
+}
 
-void CarMainWindow::ackOfRegistration()
+void CarMainWindow::errorFromServer(QNetworkReply* reply)
 {
-    qDebug() << "Server acknowledged registration";
+    qDebug() << "_errorFromServer";
+    QNetworkReply::NetworkError errorcode;
+
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        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";
+    }
+}
 
 /**
   *@brief Just for development, for the real button is not shown until
   *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 +572,25 @@ void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
 
 void CarMainWindow::on_pushButtonSendResult_clicked()
 {
-    emit sendresult();
+    sendXml();
+    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");
+    }
 }