X-Git-Url: http://git.maemo.org/git/?p=speedfreak;a=blobdiff_plain;f=Client%2Fhttpclient.cpp;h=d1b6f2bf759b377ab3990108483b0ee6efe266aa;hp=acda598757d623488279bce636091dbc183f50f9;hb=41835513c73ea950043973e697aa074a551a4eb4;hpb=191e117d32d1e3e4865f51c84b3a2bbc565a49b3 diff --git a/Client/httpclient.cpp b/Client/httpclient.cpp index acda598..d1b6f2b 100644 --- a/Client/httpclient.cpp +++ b/Client/httpclient.cpp @@ -1,70 +1,134 @@ +/* + * Http client Connects application to server. + * + * @author Tiina Kivilinna-Korhola + * @author Olavi Pulkkinen + * @author Toni Jussila + * @copyright (c) 2010 Speed Freak team + * @license http://opensource.org/licenses/gpl-license.php GNU Public License + */ + #include #include +#include #include "httpclient.h" #include "mainwindow.h" - /** - *@brief Constructor, connects object to GUI - *@param Pointer to carmainwindow, which is temporarily used during development + * Constructor, connects object to GUI + * + * @param Pointer to carmainwindow, which is temporarily used during development */ HttpClient::HttpClient(MainWindow *myCarw) { + qDebug() << "__HttpClient"; myMainw = myCarw; netManager = new QNetworkAccessManager(); myXmlwriter = new XmlWriter(); myXmlreader = new XmlReader(); + connect(myXmlreader, SIGNAL(userInfo(QStringList*)), this, SLOT(sendUsersInfo(QStringList*))); } /** - *@brief Destructor + * Destructor */ HttpClient::~HttpClient() { + qDebug() << "__~HttpClient" ; + if(myXmlwriter) + delete myXmlwriter; + if(myXmlreader) + delete myXmlreader; } /** - *@brief Sends registration information to the server in xml format. - *Reads user name, password and emaol address from resuldialogs internal variables. + * Sends registration information to the server in xml format. + * Reads user name, password and emaol address from resuldialogs internal variables. */ void HttpClient::requestRegistration() { qDebug() << "_requestRegistration" ; - qDebug() << myMainw->settingsDialog->getRegUserName() << "+" << myMainw->settingsDialog->getRegPassword() << "+" << myMainw->settingsDialog->getRegEmail(); + qDebug() << myMainw->settingsDialog->registerDialog->getRegUserName() << "+" << myMainw->settingsDialog->registerDialog->getRegPassword() << "+" << myMainw->settingsDialog->registerDialog->getRegEmail(); QBuffer *regbuffer = new QBuffer(); - QUrl qurl("http://api.speedfreak-app.com/api/register"); + QUrl qurl("http://www.speedfreak-app.com/users/register"); QNetworkRequest request(qurl); qDebug() << qurl.toString(); QNetworkReply *currentDownload; regbuffer->open(QBuffer::ReadWrite); - myXmlwriter->writeRegistering(regbuffer, + + // Without profile dialog + /*myXmlwriter->writeRegistering(regbuffer, myMainw->settingsDialog->getRegUserName(), myMainw->settingsDialog->getRegPassword(), - myMainw->settingsDialog->getRegEmail()); + myMainw->settingsDialog->getRegEmail());*/ + // With profile dialog + /*myXmlwriter->writeRegistering(regbuffer, + myMainw->settingsDialog->getRegUserName(), + myMainw->settingsDialog->getRegPassword(), + myMainw->settingsDialog->getRegEmail(), + myMainw->settingsDialog->profileDialog->getDescription()); + myMainw->settingsDialog->profileDialog->getManufacturer(), + myMainw->settingsDialog->profileDialog->getType(), + myMainw->settingsDialog->profileDialog->getModel(), + myMainw->settingsDialog->profileDialog->getDescription(), + myMainw->settingsDialog->profileDialog->getPicture());*/ + // New way: Registerdialog = register + Profiledialog + myXmlwriter->writeRegistering(regbuffer, + myMainw->settingsDialog->registerDialog->getRegUserName(), + myMainw->settingsDialog->registerDialog->getRegPassword(), + myMainw->settingsDialog->registerDialog->getRegEmail(), + myMainw->settingsDialog->registerDialog->getDescription()); + qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data(); - currentDownload = netManager->post(request, ("xml=" + regbuffer->data())); + // Registration with picture. Send picture to server + if(myMainw->settingsDialog->registerDialog->getPicture() != "" )//&& error == false) + { + qDebug() << "__Registration with picture"; + QFile pictureFile( myMainw->settingsDialog->registerDialog->getPicture() ); + if (!pictureFile.open(QIODevice::ReadOnly)) + { + qDebug() << "__picture read fail"; + return; + } + //currentDownload = netManager->post(request, pictureFile.readAll()); + currentDownload = netManager->post(request, ("xml=" + regbuffer->data(), "avatar=" + pictureFile.readAll())); + //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture())); + pictureFile.close(); + } + // Registration Without picture + else + { + qDebug() << "____Registration without picture"; + currentDownload = netManager->post(request, ("xml=" + regbuffer->data())); + } + connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration())); //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); - myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server"); + + //Indicating user + if(myMainw->settingsDialog->registerDialog) + myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Reguesting registration from server"); regbuffer->close(); } /** - *@brief Sends result(s) to the server in xml format. - *Send authentication information in the header. + * Sends result(s) to the server in xml format. + * Send authentication information in the header. */ void HttpClient::sendResultXml(QString category, double result) { qDebug() << "_sendResultXml"; + qDebug() << category; QBuffer *xmlbuffer = new QBuffer(); - QUrl qurl("http://api.speedfreak-app.com/api/update/" + category); + QUrl qurl("http://www.speedfreak-app.com/results/update/" + category); + qDebug() << qurl.toString(); QNetworkRequest request(qurl); QNetworkReply *currentDownload; @@ -82,55 +146,79 @@ void HttpClient::sendResultXml(QString category, double result) //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); //Indicating user - myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server"); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server"); xmlbuffer->close(); } /** - *@brief Sends route to the server in xml format. - *Send authentication information in the header. - *@todo Check destination URL. + * Sends route to the server in xml format. + * Send authentication information in the header. + * + * @param QString filename + * @param int 1(send to server) or 0(no send) */ -void HttpClient::sendRouteXml() +void HttpClient::sendRouteXml(QString oldName, QString newName, int i) { qDebug() << "_sendRouteXml"; - QString filename = "route.xml"; - QFile file(filename); - if (!file.open(QFile::ReadOnly)) { - qDebug() << "_sendRouteXml file.open() fail"; - return; - } + //QString filename = "/home/user/MyDocs/speedfreak/route/route.xml"; + qDebug() << "__old:" + oldName; + QString filename = newName; //+ ".xml"; - QUrl qurl("http://api.speedfreak-app.com/api/update/route"); - qDebug() << qurl.toString(); - QNetworkRequest request(qurl); - QNetworkReply *currentDownload; + if(newName != "") + { + qDebug() << "_rename xml"; + QDir dir(filename); + qDebug() << "__new:" + filename; + qDebug() << dir.rename(oldName, filename); + } - QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword(); - credentials = "Basic " + credentials.toAscii().toBase64(); - request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + if(i == 1) + { + qDebug() << "_send route"; + QFile file(filename); + if (!file.open(QFile::ReadOnly)) + { + qDebug() << "_sendRouteXml file.open() fail"; + return; + } + + QUrl qurl("http://speedfreak-app.com/update/route"); + qDebug() << qurl.toString(); + QNetworkRequest request(qurl); + QNetworkReply *currentDownload; + + QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + currentDownload = netManager->post(request, ("xml=" + file.readAll())); + connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute())); + //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); - currentDownload = netManager->post(request, ("xml=" + file.readAll())); - connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute())); - //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); - myMainw->routeDialog->setLabelInfoToUser("Sending route to server"); + //Indicating user + if(myMainw->routeSaveDialog->routeDialog) + myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server"); - file.close(); + file.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. + * 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 HttpClient::requestTopList(QString category, QString limit) { - qDebug() << "_requestTopList" ; + qDebug() << "_requestTopList"; + qDebug() << category; - QString urlBase = "http://api.speedfreak-app.com/api/results/"; + QString urlBase = "http://www.speedfreak-app.com/results/list_results/"; QUrl qurl(urlBase + category + "/" + limit); qDebug() << qurl.toString(); QNetworkRequest request(qurl); @@ -143,19 +231,21 @@ void HttpClient::requestTopList(QString category, QString limit) currentDownload = netManager->post(request, ("data=" )); connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist())); //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); - myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server"); -} + //Indicating user + if(myMainw->topResultDialog) + myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server"); +} /** - *@brief Request categories list from the server. - *Send authentication information in the header. + * Request categories list from the server. + * Send authentication information in the header. */ void HttpClient::requestCategories() { qDebug() << "_requestCategories" ; - QUrl qurl("http://api.speedfreak-app.com/api/categories/"); + QUrl qurl("http://www.speedfreak-app.com/results/categories"); qDebug() << qurl.toString(); QNetworkRequest request(qurl); QNetworkReply *currentDownload; @@ -167,20 +257,23 @@ void HttpClient::requestCategories() currentDownload = netManager->post(request, ("data=" )); connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories())); //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); - myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server"); -} + //Indicating user + if(myMainw->topResultDialog) + myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server"); +} /** - *@brief Check that username and password exist on the server. - *Send authentication information in the header. + * Check that username and password exist on the server. + * Send authentication information in the header. */ void HttpClient::checkLogin() { qDebug() << "_checkLogin"; - QUrl qurl("http://api.speedfreak-app.com/api/login/"); + QUrl qurl("http://www.speedfreak-app.com/users/login"); qDebug() << qurl.toString(); + QNetworkRequest request(qurl); QNetworkReply *currentDownload; @@ -191,19 +284,22 @@ void HttpClient::checkLogin() currentDownload = netManager->post(request, ("data=" )); connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin())); //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); - myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server"); -} + //Indicating user + if(myMainw->settingsDialog) + myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server"); +} /** - *@brief React to servers responce after result has been sent. + * React to servers responce after result has been sent. */ void HttpClient::ackOfResult() { qDebug() << "_ackOfResult"; //Indicating user - myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser(""); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser(""); QNetworkReply* reply = qobject_cast(sender()); @@ -213,24 +309,31 @@ void HttpClient::ackOfResult() qDebug() << "errorcode:" << errorcode << reply->errorString(); //Indicating user - QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString()); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString()); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error"); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled(); } else { - qDebug() << "errorcode:" << errorcode << reply->errorString(); - //Indicating user - QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll()); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll()); + if(myMainw->accstart->accRealTimeDialog->resultDialog) + myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received"); } } /** - *@brief React to servers responce after route has been sent. + * React to servers responce after route has been sent. */ void HttpClient::ackOfRoute() { qDebug() << "_ackOfRoute"; - myMainw->routeDialog->setLabelInfoToUser(""); + if(myMainw->routeSaveDialog->routeDialog) + myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser(""); QNetworkReply* reply = qobject_cast(sender()); @@ -238,23 +341,27 @@ void HttpClient::ackOfRoute() errorcode = reply->error(); if(errorcode != 0) { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->routeDialog, "Server reply to route sending ",reply->errorString()); + if(myMainw->routeSaveDialog->routeDialog) + QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString()); + if(myMainw->routeSaveDialog->routeDialog) + myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled(); } else { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll()); + if(myMainw->routeSaveDialog->routeDialog) + QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll()); } } /** - *@brief React to servers responce after registration has been sent. - *@todo Implement consequencies of reply. + * React to servers responce after registration has been sent. */ void HttpClient::ackOfRegistration() { qDebug() << "_ackOfRegistration"; - myMainw->settingsDialog->setLabelInfoToUser(""); + if(myMainw->settingsDialog) + myMainw->settingsDialog->registerDialog->setLabelInfoToUser(""); QNetworkReply* reply = qobject_cast(sender()); @@ -262,23 +369,28 @@ void HttpClient::ackOfRegistration() errorcode = reply->error(); if(errorcode != 0) { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll()); + if(myMainw->settingsDialog->registerDialog) + QMessageBox::about(myMainw->settingsDialog->registerDialog, "Server reply to registration",reply->readAll()); } else { qDebug() << "errorcode=0" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll()); + if(myMainw->settingsDialog->registerDialog) + { + QMessageBox::about(myMainw->settingsDialog->registerDialog, "Server reply to registration", "User registration " + reply->readAll()); + myMainw->settingsDialog->registerDialog->clearRegisterLineEdits(); + } } } - /** - *@brief React to servers responce after request for categories has been sent. + * React to servers responce after request for categories has been sent. */ void HttpClient::ackOfCategories() { qDebug() << "_ackOfCategories"; - myMainw->topResultDialog->setLabelInfoToUser(""); + if(myMainw->topResultDialog) + myMainw->topResultDialog->setLabelInfoToUser(""); QNetworkReply* reply = qobject_cast(sender()); myXmlreader->xmlReadCategories(reply); @@ -287,50 +399,61 @@ void HttpClient::ackOfCategories() errorcode = reply->error(); if(errorcode != 0) { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString()); + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString()); + if(myMainw->topResultDialog) + myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in."); } else { - qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK"); + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK"); + if(myMainw->topResultDialog) + myMainw->topResultDialog->setLabelInfoToUser(""); } } - /** - *@brief React to servers responce after request of TopList in certain category has been sent. + * React to servers responce after request of TopList in certain category has been sent. */ void HttpClient::ackOfLogin() { qDebug() << "_ackOffLogin"; - myMainw->settingsDialog->setLabelInfoToUser(""); + if(myMainw->settingsDialog) + myMainw->settingsDialog->setLabelInfoToUser(""); QNetworkReply* reply = qobject_cast(sender()); QNetworkReply::NetworkError errorcode; errorcode = reply->error(); - if(errorcode != 0) { + if(errorcode != 0) + { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString()); + if(myMainw->settingsDialog) + { + QMessageBox::about(myMainw->settingsDialog, "Wrong username or password!","Please try again."); + myMainw->settingsDialog->usernameOk(false); + } } - else { + else + { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll()); + if(myMainw->settingsDialog) + QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll()); + // here signal emit to mainwindow for username setting to main panel + emit loginOK(); + if( myMainw->settingsDialog) + { + myMainw->settingsDialog->usernameOk(true); + myMainw->settingsDialog->close(); + } } - - myMainw->settingsDialog->close(); } - /** - *@brief Reports errors, when server has sent error signal. + * Reports errors, when server has sent error signal. */ void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode) { qDebug() << "_errorFromServer"; - - //myMainw->setLabelInfoToUser(""); - QNetworkReply* reply = qobject_cast(sender()); if(errorcode != 0) { @@ -344,28 +467,291 @@ void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode) } } - /** - *@brief React to servers responce after request of TopList in certain category has been sent. + * React to servers responce after request of TopList in certain category has been sent. */ void HttpClient::ackOfToplist() { qDebug() << "_ackOfToplist"; + QNetworkReply* reply = qobject_cast(sender()); + myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName()); + + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //Indicating user + if(myMainw->topResultDialog) + { + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString()); + myMainw->topResultDialog->setLabelInfoToUser("No results ;("); + } + } + else { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //Indicating user + if(myMainw->topResultDialog) + { + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll()); + myMainw->topResultDialog->setLabelInfoToUser(""); + } + } +} + +/** + * This function sends profile to the server in xml format. + * Send authentication information in the header. + */ +void HttpClient::sendProfileXml() +{ + qDebug() << "_sendProfileXml"; + + QString userName = myMainw->settingsDialog->getUserName(); + QString filename = userName + "_profile.xml"; + QFile file(filename); + if (!file.open(QFile::ReadWrite | QFile::Text)) + { + qDebug() << "_xmlWrite fail"; + return; + } + myXmlwriter->writeProfileXmlFile(&file, userName, + myMainw->settingsDialog->registerDialog->getManufacturer(), + myMainw->settingsDialog->registerDialog->getType(), + myMainw->settingsDialog->registerDialog->getModel(), + myMainw->settingsDialog->registerDialog->getDescription(), + myMainw->settingsDialog->registerDialog->getPicture()); + + //Indicating user + if(myMainw->settingsDialog->registerDialog) + myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile saved to phone"); + + // Send xml to server + /*QUrl qurl("http://speedfreak-app.com/api/profile"); + QNetworkRequest request(qurl); + qDebug() << qurl.toString(); + QNetworkReply *currentDownload; + + QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + currentDownload = netManager->post(request, ("xml=" + file.readAll())); + bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/ + + file.close(); + + // Send picture to server + /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false) + { + QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() ); + if (!pictureFile.open(QIODevice::ReadOnly)) + { + qDebug() << "__picture read fail"; + return; + } + currentDownload = netManager->post(request, pictureFile.readAll()); + connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture())); + pictureFile.close(); + }*/ +} + +/** + * This slot function react to servers responce after request of profile has been sent. + */ +bool HttpClient::ackOfProfile() +{ + qDebug() << "__ackOfProfile"; + QNetworkReply* reply = qobject_cast(sender()); + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //Indicating user + if(myMainw->settingsDialog->registerDialog) + { + //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString()); + myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile save to server - fail"); + return true; + } + } + else { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //Indicating user + if(myMainw->settingsDialog->registerDialog) + { + //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll()); + myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile saved to server"); + return false; + } + } +} +/** + * This slot function react to servers responce after request of picture has been sent. + */ +void HttpClient::ackOfSendingPicture() +{ + qDebug() << "__ackOfSendingPicture"; + QNetworkReply* reply = qobject_cast(sender()); + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //Indicating user + if(myMainw->settingsDialog->registerDialog) + { + //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString()); + myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Picture save to server - fail"); + } + } + else { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //Indicating user + if(myMainw->settingsDialog->registerDialog) + { + //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll()); + myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Picture saved to server"); + } + } +} + +/** + * Request the user information of certain user from the server. + * Send authentication information in the header. + * + * @param QString username which information we want. + */ +void HttpClient::requestUserInfo(QString username) +{ + qDebug() << "_requestUsersInfo" ; + + QUrl qurl("http://speedfreak-app.com/users/info/" + username); + qDebug() << qurl.toString(); + QNetworkRequest request(qurl); + QNetworkReply *currentDownload; + + QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + currentDownload = netManager->post(request, ("data=" )); + connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfUserInfo())); + + //qDebug() << "requestUserInfo"; + //ackOfUserInfo(); +} + +/** + * React to servers responce after request the user information of certain user. + */ +void HttpClient::ackOfUserInfo() +{ + qDebug() << "ackUserInfo"; + /*QString fileName = "user.xml"; + QFile file(fileName); + //file.setFileName( "routetemp.xml"); + if (!file.open(QFile::ReadOnly)) + { + qDebug() << "_xmlShow fail"; + return; + } - //myMainw->setLabelInfoToUser(""); + myXmlreader->xmlReadUserInfo(&file); + file.close();*/ QNetworkReply* reply = qobject_cast(sender()); - myXmlreader->xmlReadTop10Results(reply); + //myXmlreader->xmlReadUserInfo(reply); + //for(int i = 0; i < myXmlreader->usersList->count(); i++) + //{ + // myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i)); + //} QNetworkReply::NetworkError errorcode; errorcode = reply->error(); if(errorcode != 0) { qDebug() << "errorcode:" << errorcode << reply->errorString(); - QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString()); + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString()); + if(myMainw->usersDialog) + myMainw->usersDialog->setLabelInfoToUser("You're not logged! Please register or log in."); } else { + myXmlreader->xmlReadUserInfo(reply); qDebug() << "errorcode:" << errorcode << reply->errorString(); - //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll()); + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK"); + if(myMainw->usersDialog) + myMainw->usersDialog->setLabelInfoToUser(""); } } +/** + * Request the users list of all users from the server. + * Send authentication information in the header. + */ +void HttpClient::requestUsers() +{ + qDebug() << "_requestUsers" ; + + QUrl qurl("http://www.speedfreak-app.com/users/list_all"); + qDebug() << qurl.toString(); + QNetworkRequest request(qurl); + QNetworkReply *currentDownload; + + QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + currentDownload = netManager->post(request, ("data=" )); + connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfUsers())); + + + //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError))); + + //Indicating user + if(myMainw->usersDialog) + myMainw->usersDialog->setLabelInfoToUser("Reguesting users from server"); + + //ackOfUsers(); +} + +/** + * React to servers responce after request the users list of all users. + */ +void HttpClient::ackOfUsers() +{ + qDebug() << "ackUsers"; + + //if(myMainw->topResultDialog) + // myMainw->topResultDialog->setLabelInfoToUser(""); + + QNetworkReply* reply = qobject_cast(sender()); + //myXmlreader->xmlReadUsers(reply); + + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString()); + if(myMainw->usersDialog) + myMainw->usersDialog->setLabelInfoToUser("You're not logged! Please register or log in."); + } + else { + myXmlreader->xmlReadUsers(reply); + qDebug() << "errorcode:" << errorcode << reply->errorString(); + //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK"); + if(myMainw->usersDialog) + myMainw->usersDialog->setLabelInfoToUser(""); + for(int i = 0; i < myXmlreader->usersList->count(); i++) + { + myMainw->usersDialog->appendUserToList(myXmlreader->usersList->at(i)); + } + } +} + +/** + * This slot function called when userInfo signal is emitted from xmlreader. + * + * @param QStringList usersInfo includes information from certain user. + */ +void HttpClient::sendUsersInfo(QStringList* usersInfo) +{ + myMainw->usersDialog->setUserInfo(usersInfo); +}