-/**
- * CarMainWindow main class
- *
- * @author Toni Jussila <toni.jussila@fudeco.com>
- * @author Janne Änäkkälä <janne.anakkala@fudeco.com>
- * @author Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
- * @author Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
- * @copyright (c) 2010 Speed Freak team
- * @license http://opensource.org/licenses/gpl-license.php GNU Public License
- */
+/*
+ * CarMainWindow main class
+ *
+ * @author Toni Jussila <toni.jussila@fudeco.com>
+ * @author Janne Änäkkälä <janne.anakkala@fudeco.com>
+ * @author Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
+ * @author Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @copyright (c) 2010 Speed Freak team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
#include "carmainwindow.h"
#include "math.h"
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();
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;
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");
+
}
/**
//measure = NULL;
//measure = new MeasureDialog();
// connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
+ accelerometer->start();
timer->start();
// Show measure dialog.
//measure->show();
//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: ";
}
/**
- *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()
*/
void CarMainWindow::on_buttonTopRefresh_clicked()
{
- setCategoryCompoBox();
+ //setCategoryCompoBox();
+ requestTopList();
}
/**
void CarMainWindow::on_setUserPushButton_clicked()
{
myLogin->show();
-
- ui->userNameLabel->setText( "User: " + myLogin->getUserName());
- ui->setUserPushButton->setText( "Change User");
}
/**
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(),
//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();
}
/**
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);
+
+ QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
+ credentials = "Basic " + credentials.toAscii().toBase64();
- //ackFromServer function gets called when HTTP request is completed
- connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
+ 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();
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");
+ }
}