X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=Client%2Fcarmainwindow.cpp;h=a3aa67a8d8523088983af3274384d0080e96e490;hb=3932141aa4e6ae8599d87b0f520b968ec9889266;hp=47859e1615585a294030bc2b5cc37200e7c34184;hpb=7eddd25d4c909ac6938335e25f53594912cfbf07;p=speedfreak diff --git a/Client/carmainwindow.cpp b/Client/carmainwindow.cpp index 47859e1..a3aa67a 100644 --- a/Client/carmainwindow.cpp +++ b/Client/carmainwindow.cpp @@ -30,9 +30,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 +38,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 +106,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(); @@ -211,32 +212,13 @@ void CarMainWindow::openResultView() //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult); } -/** - *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"); } /** @@ -252,7 +234,8 @@ void CarMainWindow::on_registratePushButton_clicked() */ void CarMainWindow::on_buttonTopRefresh_clicked() { - setCategoryCompoBox(); + //setCategoryCompoBox(); + requestTopList(); } /** @@ -296,16 +279,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 +291,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 +309,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); + + 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(); @@ -516,5 +582,25 @@ void CarMainWindow::on_pushButtonMeasureTabAbort_clicked() void CarMainWindow::on_pushButtonSendResult_clicked() { - emit sendresult(); + sendXml(); + +} + +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"); + } }