Added functionality for other users listing and viewing other users info.
authorJanne Änäkkälä <janne_anakkala@hotmail.com>
Wed, 26 May 2010 11:43:20 +0000 (14:43 +0300)
committerJanne Änäkkälä <janne_anakkala@hotmail.com>
Wed, 26 May 2010 11:43:20 +0000 (14:43 +0300)
Client/httpclient.cpp
Client/httpclient.h
Client/mainwindow.cpp
Client/mainwindow.h
Client/mainwindow.ui
Client/xmlreader.cpp
Client/xmlreader.h

index f200355..08e1695 100644 (file)
@@ -25,6 +25,7 @@ HttpClient::HttpClient(MainWindow *myCarw)
     netManager = new QNetworkAccessManager();
     myXmlwriter = new XmlWriter();
     myXmlreader = new XmlReader();
+    connect(myXmlreader, SIGNAL(userInfo(QStringList*)), this, SLOT(sendUsersInfo(QStringList*)));
 }
 
 /**
@@ -574,3 +575,145 @@ void HttpClient::ackOfSendingPicture()
         }
     }
 }
+
+/**
+  *@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);
+}
index 457f7b7..f0eed3f 100644 (file)
@@ -52,6 +52,12 @@ public slots:
     void ackOfSendingPicture();
     void errorFromServer(QNetworkReply::NetworkError);
     void sendProfileXml();
+    void requestUserInfo(QString username);
+    void ackOfUserInfo();
+
+    void requestUsers();
+    void ackOfUsers();
+    void sendUsersInfo(QStringList* usersInfo);
 
 };
 
index 04aeb97..27a1f4b 100644 (file)
@@ -34,6 +34,8 @@ MainWindow::MainWindow(QWidget *parent) :
     routeSaveDialog = NULL;
     topResultDialog = NULL;
 
+    usersDialog = NULL;
+
     settingsDialog = new SettingsDialog;
     connect(settingsDialog, SIGNAL(sendregistration()), this, SLOT(clientRegUserToServer()));
     connect(settingsDialog, SIGNAL(userNameChanged()),  this, SLOT(clientUserLogin()));
@@ -429,3 +431,38 @@ void MainWindow::saveProfile()
     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();
+}
index 509809d..9a5854e 100644 (file)
@@ -27,6 +27,7 @@
 #include "resultdialog.h"
 #include "helpdialog.h"
 #include "custombutton.h"
+#include "usersdialog.h"
 
 
 namespace Ui {
@@ -48,6 +49,7 @@ public:
     TopResultDialog *topResultDialog;
     HttpClient *httpClient;
     HelpDialog *helpDialog;
+    UsersDialog *usersDialog;
 
 protected:
     void changeEvent(QEvent *e);
@@ -64,6 +66,7 @@ private:
     CustomButton* customButtonHelp;
 
 private slots:
+    void on_pushButtonUsers_clicked();    
     void clientRequestCategoryList();
     void clientRequestTopList(int index);
     void clientSendRoute(QString,QString,int);
@@ -81,6 +84,8 @@ private slots:
     void OpenWWWPage();
     void OpenHelpDialog();
     void OpenSettingsDialog();
+    void requestGetUserInfo(QString);
+    void requestGetUsers();
 };
 
 #endif // MAINWINDOW_H
index 4ad8998..84af0cd 100644 (file)
      <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">
index 9c7766b..3341edf 100644 (file)
@@ -19,6 +19,8 @@ XmlReader::XmlReader()
 {
     qDebug() << "__XmlReader";
     myCategoryList = new CategoryList();
+    usersList = NULL;
+    usersInfo = new QStringList();
 }
 
 /**
@@ -263,3 +265,116 @@ void XmlReader::xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog)
     }
     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);
+            }
+        }
+    }
+}
index 6ba8f66..6dbab29 100644 (file)
@@ -24,6 +24,7 @@ public:
     ~XmlReader();
     CategoryList *myCategoryList;
     void xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog);
+    QStringList *usersList;
 
 private:
     QXmlStreamReader xmlreader;
@@ -36,16 +37,23 @@ private:
     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