netManager = new QNetworkAccessManager();
myXmlwriter = new XmlWriter();
myXmlreader = new XmlReader();
+ connect(myXmlreader, SIGNAL(userInfo(QStringList*)), this, SLOT(sendUsersInfo(QStringList*)));
}
/**
}
}
}
+
+/**
+ *@brief Request the user information of certain user from the server.
+ *Send authentication information in the header.
+ *@param 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();
+}
+
+/**
+ *@brief 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;
+ }
+
+ myXmlreader->xmlReadUserInfo(&file);
+ file.close();*/
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+ myXmlreader->xmlReadUserInfo(reply);
+
+ //for(int i = 0; i < myXmlreader->usersList->count(); i++)
+ //{
+ // myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i));
+ //}
+}
+
+/**
+ *@brief 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->topResultDialog)
+ //myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
+
+ //ackOfUsers();
+}
+
+/**
+ *@brief React to servers responce after request the users list of all users.
+ */
+void HttpClient::ackOfUsers()
+{
+ qDebug() << "ackUsers";
+ /* QString fileName = "jtn.xml";
+ QFile file(fileName);
+ //file.setFileName( "routetemp.xml");
+ if (!file.open(QFile::ReadOnly))
+ {
+ qDebug() << "_xmlShow fail";
+ return;
+ }
+
+ myXmlreader->xmlReadUsers(&file);
+ file.close();
+
+ for(int i = 0; i < myXmlreader->usersList->count(); i++)
+ {
+ myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i));
+ }*/
+
+ qDebug() << "ackUsers";
+
+ //if(myMainw->topResultDialog)
+ // myMainw->topResultDialog->setLabelInfoToUser("");
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+ myXmlreader->xmlReadUsers(reply);
+
+ for(int i = 0; i < myXmlreader->usersList->count(); i++)
+ {
+ myMainw->usersDialog->appendUserToList(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 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");
+ if(myMainw->topResultDialog)
+ myMainw->topResultDialog->setLabelInfoToUser("");
+ }*/
+}
+
+/**
+ * This slot function called when userInfo signal is emitted from xmlreader.
+ *@param usersInfo includes information from certain user.
+ */
+void HttpClient::sendUsersInfo(QStringList* usersInfo)
+{
+ myMainw->usersDialog->setUserInfo(usersInfo);
+}
void ackOfSendingPicture();
void errorFromServer(QNetworkReply::NetworkError);
void sendProfileXml();
+ void requestUserInfo(QString username);
+ void ackOfUserInfo();
+
+ void requestUsers();
+ void ackOfUsers();
+ void sendUsersInfo(QStringList* usersInfo);
};
routeSaveDialog = NULL;
topResultDialog = NULL;
+ usersDialog = NULL;
+
settingsDialog = new SettingsDialog;
connect(settingsDialog, SIGNAL(sendregistration()), this, SLOT(clientRegUserToServer()));
connect(settingsDialog, SIGNAL(userNameChanged()), this, SLOT(clientUserLogin()));
if(httpClient)
httpClient->sendProfileXml();
}
+
+/**
+ * This slot function calls httpClients requestUserInfo for getting user's information from server.
+ */
+void MainWindow::requestGetUserInfo(QString name)
+{
+ qDebug() << "getUserInfo signal " + name;
+ if(httpClient)
+ {
+ httpClient->requestUserInfo(name);
+ }
+}
+
+/**
+ * This slot function calls httpClients requestUsers for getting usernames from server.
+ */
+void MainWindow::requestGetUsers()
+{
+ qDebug() << "getUsers signal";
+ if(httpClient)
+ {
+ httpClient->requestUsers();
+ }
+}
+
+void MainWindow::on_pushButtonUsers_clicked()
+{
+ if(!usersDialog)
+ usersDialog = new UsersDialog;
+
+ connect(usersDialog, SIGNAL(getUserInfo(QString)), this, SLOT(requestGetUserInfo(QString)));
+ //connect(usersDialog, SIGNAL(getUsers()), this, SLOT(requestGetUsers()));
+ requestGetUsers();
+ usersDialog->show();
+}
#include "resultdialog.h"
#include "helpdialog.h"
#include "custombutton.h"
+#include "usersdialog.h"
namespace Ui {
TopResultDialog *topResultDialog;
HttpClient *httpClient;
HelpDialog *helpDialog;
+ UsersDialog *usersDialog;
protected:
void changeEvent(QEvent *e);
CustomButton* customButtonHelp;
private slots:
+ void on_pushButtonUsers_clicked();
void clientRequestCategoryList();
void clientRequestTopList(int index);
void clientSendRoute(QString,QString,int);
void OpenWWWPage();
void OpenHelpDialog();
void OpenSettingsDialog();
+ void requestGetUserInfo(QString);
+ void requestGetUsers();
};
#endif // MAINWINDOW_H
<bool>true</bool>
</property>
</widget>
+ <widget class="QPushButton" name="pushButtonUsers">
+ <property name="geometry">
+ <rect>
+ <x>170</x>
+ <y>20</y>
+ <width>93</width>
+ <height>27</height>
+ </rect>
+ </property>
+ <property name="text">
+ <string>Users</string>
+ </property>
+ </widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
{
qDebug() << "__XmlReader";
myCategoryList = new CategoryList();
+ usersList = NULL;
+ usersInfo = new QStringList();
}
/**
}
profile = NULL;
}
+
+/**
+ *This function is used to parse user's info of a certain username.
+ */
+//void XmlReader::xmlReadUserInfo(QIODevice *device)
+void XmlReader::xmlReadUserInfo(QNetworkReply *device)
+{
+ /* <?xml version="1.0" encoding="utf-8"?>
+ <userinfo user="test928" manufacturer="Toyota" type="corolla" model="1983" description="Fuel
+ efficient, GPS system, only one owner"/>
+ */
+
+ /* <?xml version="1.0" encoding="utf-8"?>
+ <user login="test" description="<![CDATA my car rox! ]]>" last_activity="2010-05-13 21:47:15"
+ avatar="http://www.speedfreak-api.com/static/uploads/avatars/22.jpg" />*/
+
+ usersInfo->clear();
+ xmlreader.clear();
+ QByteArray array = device->readAll();
+ qDebug() << "array: " << array;
+ xmlreader.addData(array);
+
+ while(!xmlreader.atEnd())
+ {
+ //Read next node
+ xmlreader.readNext();
+
+ //Check if this element is starting element
+ if(xmlreader.isStartElement())
+ {
+ if(xmlreader.name() == "user")
+ {
+ qDebug() << xmlreader.name();
+ attr = xmlreader.attributes();
+ QString data;
+ data = attr.value("login").toString();
+ qDebug() << "user: " << data;
+ usersInfo->append(data);
+ data = attr.value("description").toString();
+ qDebug() << "description: " << data;
+ usersInfo->append(data);
+
+ /*data = attr.value("type").toString();
+ qDebug() << "type: " << data;
+ usersInfo.append(data);
+ data = attr.value("model").toString();
+ qDebug() << "model: " << data;
+ usersInfo.append(data);
+ data = attr.value("description").toString();
+ qDebug() << "description: " << data;
+ usersInfo.append(data);*/
+ //usersList->append(username);
+ }
+ }
+ }
+ qDebug() << "__emit";
+ emit userInfo(usersInfo);
+}
+
+/**
+ *This function is used to parse usernames.
+ */
+void XmlReader::xmlReadUsers(QNetworkReply *device)
+//void XmlReader::xmlReadUsers(QIODevice *device)
+{
+ /* <?xml version="1.0" encoding="utf-8"?>
+ <users>
+ <user login="test" description="<![CDATA my car rox! ]]>" last_activity="2010-05-13 21:47:15" />
+ <user login="test1" description="<![CDATA my car rox too! ]]>" last_activity="2010-05-13 21:28:00" />
+ </users>*/
+
+ xmlreader.clear();
+ QByteArray array = device->readAll();
+ qDebug() << "array: " << array;
+ xmlreader.addData(array);
+ //Go trough the xml document
+
+ if (!usersList)
+ {
+ usersList = new QStringList();
+ qDebug() << "userlist luodaan";
+ }
+
+ else
+ {
+ usersList->clear();
+ qDebug() << "userlist tyhjennetaan";
+ }
+
+ while(!xmlreader.atEnd())
+ {
+ //Read next node
+ xmlreader.readNext();
+ qDebug() << xmlreader.name();
+
+ //Check if this element is starting element
+ if(xmlreader.isStartElement())
+ {
+ if (xmlreader.name() == "user")
+ {
+ qDebug() << xmlreader.name();
+ attr = xmlreader.attributes();
+ QString username;
+ QString description;
+ username = attr.value("login").toString();
+ description = attr.value("description").toString();
+ qDebug() << "user: " << username;
+ qDebug() << "description: " << description;
+ usersList->append(username);
+ }
+ }
+ }
+}
~XmlReader();
CategoryList *myCategoryList;
void xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog);
+ QStringList *usersList;
private:
QXmlStreamReader xmlreader;
QString user;
QString value;
QString description;
+ QStringList *usersInfo;
signals:
void receivedCategoryList();
void receivedTop10List();
+ void userInfo(QStringList *userInfo);
public slots:
void xmlReadTop10Results(QNetworkReply *device, QString userName);
void xmlReadCategories(QNetworkReply *device);
//void xmlReadCategories(QIODevice *device);
void xmlShow();
+
+ //void xmlReadUserInfo(QIODevice *device);
+ void xmlReadUserInfo(QNetworkReply *device);
+ void xmlReadUsers(QNetworkReply *device);
+ //void xmlReadUsers(QIODevice *device);
};
#endif // XMLREADER_H