Changed fetchPeopleWithSimilarInterest to send request to Situare server instead...
authorJussi Laitinen <jussi.laitinen@ixonos.com>
Tue, 8 Mar 2011 17:12:29 +0000 (19:12 +0200)
committerJussi Laitinen <jussi.laitinen@ixonos.com>
Tue, 8 Mar 2011 17:12:29 +0000 (19:12 +0200)
src/engine/engine.cpp
src/situareservice/situarecommon.h
src/situareservice/situareservice.cpp
src/situareservice/situareservice.h
src/ui/friendlistpanel.cpp
src/ui/friendlistpanel.h
src/ui/mainwindow.cpp
src/user/user.cpp
src/user/user.h

index db4f662..1658c1b 100644 (file)
@@ -452,11 +452,12 @@ void SituareEngine::requestInterestingPeople()
     qDebug() << __PRETTY_FUNCTION__;
 
     QRectF currentSceneRect = m_mapEngine->currentViewSceneRect();
-    SceneCoordinate bottomLeftSceneCoordinate(currentSceneRect.left(), currentSceneRect.bottom());
-    SceneCoordinate topRightSceneCoordinate(currentSceneRect.right(), currentSceneRect.top());
+    GeoCoordinate centerGeoCoordinate = m_mapEngine->centerGeoCoordinate();
+    SceneCoordinate topCenterSceneCoordinate(
+                currentSceneRect.left() - currentSceneRect.width() / 2, currentSceneRect.top());
 
-    m_situareService->fetchPeopleWithSimilarInterest(GeoCoordinate(bottomLeftSceneCoordinate),
-                                                     GeoCoordinate(topRightSceneCoordinate));
+    m_situareService->fetchPeopleWithSimilarInterest(
+                centerGeoCoordinate.distanceTo(GeoCoordinate(topCenterSceneCoordinate)));
 }
 
 void SituareEngine::requestSendMessage(const QString &receiverId, const QString &message,
index d4e4b5a..a3df703 100644 (file)
@@ -46,6 +46,7 @@ const QString GET_LOCATIONS = "getLocations.php";
 const QString GET_MESSAGES = "getMessages.php";
 const QString SEND_MESSAGE = "sendMessage.php";
 const QString REMOVE_MESSAGE = "removeMessage.php";
+const QString GET_PEOPLE_WITH_SIMILAR_INTEREST = "getInterestingPeople.php";
 
 // Cookies
 const QString COOKIE = "Cookie";
index 9b969ee..8ad5420 100644 (file)
@@ -177,17 +177,15 @@ void SituareService::fetchMessages()
     buildRequest(GET_MESSAGES, QHash<QString, QString>());
 }
 
-void SituareService::fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
-                                                    const GeoCoordinate &northEastCoordinates)
+void SituareService::fetchPeopleWithSimilarInterest(const qreal distance)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    //Request sent to server does not need the UID
-    QByteArray arr = m_database->getInterestingPeople(613374451,
-                                                      southWestCoordinates,
-                                                      northEastCoordinates);
+    const int TO_KM_DIVIDER = 1000;
+    QHash<QString, QString> parameters;
+    parameters.insert("distance", degreesToString(distance / TO_KM_DIVIDER));
 
-    parseInterestingPeopleData(arr);
+    buildRequest(GET_PEOPLE_WITH_SIMILAR_INTEREST, parameters);
 }
 
 void SituareService::fetchPopularTags()
@@ -225,6 +223,8 @@ SituareService::RequestName SituareService::getRequestName(const QUrl &url) cons
         return SituareService::RequestRemoveMessage;
     else if (url.toString().contains(SEND_MESSAGE))
         return SituareService::RequestSendMessage;
+    else if (url.toString().contains(GET_PEOPLE_WITH_SIMILAR_INTEREST))
+        return SituareService::RequestGetPeopleWithSimilarInterest;
     else
         return SituareService::RequestUnknown;
 }
@@ -246,81 +246,72 @@ void SituareService::imageReceived(const QString &id, const QPixmap &image)
         emit imageReady(id, AvatarImage::create(image, AvatarImage::Small));
 }
 
-void SituareService::parseInterestingPeopleData(const QByteArray &jsonReply)
+void SituareService::parseInterestingPeopleData(const QVariant &interestingPeopleData)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QJson::Parser parser;
-    bool ok;
+    QVariant people = interestingPeopleData.toMap();
 
-    QVariantMap result = parser.parse(jsonReply, &ok).toMap();
+    QList<User> friends;
+    QList<User> others;
 
-    if (!ok) {
-        emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
-        return;
-    } else {
-        QVariant people = result["people"];
+    foreach (QVariant personVariant, people.toMap().value("friends").toList()) {
+        User user;
+        QMap<QString, QVariant> person = personVariant.toMap();
+        user.setUserId(person["uid"].toString());
+        user.setName(person["name"].toString());
+        user.setProfileImage(AvatarImage::create(
+                QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
+        user.setProfileImageUrl(person["pic_square"].toUrl());
+        user.setTimestamp(person["timestamp"].toString());
+        user.setTags(person["tags"].toString());
+        user.setAddress(person["address"].toString());
+        user.setNote(person["note"].toString());
 
-        QList<User> friends;
-        QList<User> others;
+        bool latOk;
+        qreal latitude = person["lat"].toReal(&latOk);
+        bool lonOk;
+        qreal longitude = person["lon"].toReal(&lonOk);
 
-        foreach (QVariant personVariant, people.toMap().value("friends").toList()) {
-            User user;
-            QMap<QString, QVariant> person = personVariant.toMap();
-            user.setUserId(person["uid"].toString());
-            user.setName(person["name"].toString());
-            user.setProfileImage(AvatarImage::create(
-                    QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
-            user.setProfileImageUrl(person["image_url"].toUrl());
-            user.setTags(person["tags"].toList());
-
-            bool latOk;
-            qreal latitude = person["latitude"].toReal(&latOk);
-            bool lonOk;
-            qreal longitude = person["longitude"].toReal(&lonOk);
-
-            if (latOk && lonOk) {
-                user.setCoordinates(GeoCoordinate(latitude, longitude));
-
-                //This should be from the server
-                GeoCoordinate myCoord(65.008, 25.5523);
-                qreal meters = myCoord.distanceTo(user.coordinates());
-                qreal value;
-                QString unit;
-                if (meters < 1000) {
-                    value = meters;
-                    unit = "m";
-                } else {
-                    value = meters/1000;
-                    unit = "km";
-                }
-                user.setDistance(value, unit);
+        if (latOk && lonOk) {
+            user.setCoordinates(GeoCoordinate(latitude, longitude));
+
+            qreal meters = person["distance"].toReal() * 1000;
+            qreal value;
+            QString unit;
+            if (meters < 1000) {
+                value = meters;
+                unit = "m";
+            } else {
+                value = meters/1000;
+                unit = "km";
             }
-
-            friends.append(user);
-
-            //Remove comment when the actual server is used
-            //emit fetchImage(user.userId(), user.profileImageUrl());
+            user.setDistance(value, unit);
         }
 
-        foreach (QVariant personVariant, people.toMap().value("others").toList()) {
-            User user;
-            QMap<QString, QVariant> person = personVariant.toMap();
-            user.setUserId(person["uid"].toString());
-            user.setName(person["name"].toString());
-            user.setProfileImage(AvatarImage::create(
-                    QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
-            user.setProfileImageUrl(person["image_url"].toUrl());
-            user.setTags(person["tags"].toList());
+        friends.append(user);
 
-            others.append(user);
+        //Remove comment when the actual server is used
+        //emit fetchImage(user.userId(), user.profileImageUrl());
+    }
 
-            //Remove comment when the actual server is used
-            //emit fetchImage(user.userId(), user.profileImageUrl());
-        }
+    foreach (QVariant personVariant, people.toMap().value("others").toList()) {
+        User user;
+        QMap<QString, QVariant> person = personVariant.toMap();
+        user.setUserId(person["uid"].toString());
+        user.setName(person["name"].toString());
+        user.setProfileImage(AvatarImage::create(
+                QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
+        user.setProfileImageUrl(person["pic_square"].toUrl());
+        user.setTimestamp(person["timestamp"].toString());
+        user.setTags(person["tags"].toString());
+
+        others.append(user);
 
-        emit interestingPeopleReceived(friends, others);
+        emit fetchImage(user.userId(), user.profileImageUrl());
     }
+
+    emit interestingPeopleReceived(friends, others);
 }
 
 void SituareService::parseReply(const QByteArray &jsonReply, RequestName requestName)
@@ -354,6 +345,8 @@ void SituareService::parseReply(const QByteArray &jsonReply, RequestName request
                 parseMessagesData(resultData);
             else if (requestName == SituareService::RequestSendMessage)
                 emit updateWasSuccessful((SituareService::SuccessfulSendMessage));
+            else if (requestName == SituareService::RequestGetPeopleWithSimilarInterest)
+                parseInterestingPeopleData(resultData);
         }
     }
 }
@@ -679,8 +672,16 @@ void SituareService::sendMessage(const QString &receiverId, const QString &messa
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (m_database->sendMessage(613374451, receiverId.toULongLong(), message, coordinates))
-        emit updateWasSuccessful(SituareService::SuccessfulSendMessage);
+    QHash<QString, QString> parameters;
+    parameters.insert("rid", receiverId);
+    parameters.insert("message", message);
+
+    if (coordinates.isValid()) {
+        parameters.insert("lat", degreesToString(coordinates.latitude()));
+        parameters.insert("lon", degreesToString(coordinates.longitude()));
+    }
+
+    buildRequest(SEND_MESSAGE, parameters);
 }
 
 void SituareService::sendRequest(const QString &requestUrl)
index 350884d..90d22ad 100644 (file)
@@ -59,7 +59,8 @@ private:
                        RequestReverseGeo,
                        RequestGetMessages,
                        RequestRemoveMessage,
-                       RequestSendMessage};
+                       RequestSendMessage,
+                       RequestGetPeopleWithSimilarInterest};
 
 public:
 
@@ -91,12 +92,10 @@ public:
     /**
     * @brief Retrieves people with similart interest (same tags).
     *
-    * People is searched from area defined by south-west and north-east bounds.
-    * @param southWest south-west coordinates of bounds
-    * @param northEast north-east coordinates of bounds
+    * People is searched from area defined by maximum distance
+    * @param maximumDistance maximum distance to people
     */
-    void fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
-                                        const GeoCoordinate &northEastCoordinates);
+    void fetchPeopleWithSimilarInterest(const qreal distance);
 
     /**
     * @brief Retrieves location user and friends information from Situare server
@@ -247,9 +246,9 @@ private:
     /**
     * @brief Parses interesting people data from JSON string
     *
-    * @param jsonReply JSON string
+    * @param interestingPeopleData interesting people data QVariant tree
     */
-    void parseInterestingPeopleData(const QByteArray &jsonReply);
+    void parseInterestingPeopleData(const QVariant &interestingPeopleData);
 
     /**
     * @brief Parses messages data from JSON string
index e473cfc..8634e05 100644 (file)
@@ -140,6 +140,12 @@ FriendListPanel::FriendListPanel(QWidget *parent)
     connect(m_showContactButton, SIGNAL(clicked()),
             this, SLOT(requestSelectedFriendContactDialog()));
 
+    ImageButton *messageButton = new ImageButton(":/res/images/chat_btn.png",
+                                                 ":/res/images/chat_btn_s.png",
+                                                 ":/res/images/chat_btn_d.png", this);
+    connect(messageButton, SIGNAL(clicked()),
+            this, SLOT(messageButtonPressed()));
+
     m_clearGroupFilteringButton = new ImageButton(":res/images/filtered.png",
                                                   ":res/images/filtered_s.png",
                                                   ":res/images/filtered_d.png", this);
@@ -150,6 +156,7 @@ FriendListPanel::FriendListPanel(QWidget *parent)
 
     m_itemButtonsLayout->addWidget(m_routeButton);
     m_itemButtonsLayout->addWidget(m_showContactButton);
+    m_itemButtonsLayout->addWidget(messageButton);
     m_genericButtonsLayout->addWidget(m_clearGroupFilteringButton);
 
     showEmptyPanel(true);
@@ -259,6 +266,19 @@ void FriendListPanel::hideEvent(QHideEvent *event)
     m_friendListView->clearItemSelection();
 }
 
+void FriendListPanel::messageButtonPressed()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    FriendListItem *item = dynamic_cast<FriendListItem *>(m_friendListView->selectedItem());
+
+    if (item) {
+        m_friendListView->clearItemSelection();
+        emit requestMessageDialog(QPair<QString, QString>(item->facebookId(),
+                                                          item->title()));
+    }
+}
+
 void FriendListPanel::requestSelectedFriendContactDialog()
 {
      qDebug() << __PRETTY_FUNCTION__;
index 9988cb5..2bd679e 100644 (file)
@@ -163,6 +163,13 @@ private slots:
     void filterTextChanged(const QString &text);
 
     /**
+    * @brief Called when message button is pressed.
+    *
+    * Calls requestMessageDialog with message receiver's ID and name
+    */
+    void messageButtonPressed();
+
+    /**
     * @brief Requests selected friend's contact dialog.
     */
     void requestSelectedFriendContactDialog();
@@ -210,6 +217,13 @@ signals:
     void findFriend(const GeoCoordinate &coordinates);
 
     /**
+    * @brief Requests message dialog.
+    *
+    * @param receiver receiver facebook ID and name
+    */
+    void requestMessageDialog(const QPair<QString, QString> &receiver);
+
+    /**
     * @brief Requests contact dialog.
     *
     * @param facebookId contact's facebookId
index 91258c6..f0f9c78 100644 (file)
@@ -193,6 +193,9 @@ void MainWindow::buildFriendListPanel()
 
     connect(m_friendsListPanel, SIGNAL(requestContactDialog(const QString &)),
             this, SIGNAL(requestContactDialog(const QString &)));
+
+    connect(m_friendsListPanel, SIGNAL(requestMessageDialog(QPair<QString, QString>)),
+            this, SIGNAL(requestMessageDialog(QPair<QString, QString>)));
 }
 
 void MainWindow::buildFullScreenButton()
index c4d5537..5cbd4fe 100644 (file)
@@ -107,6 +107,17 @@ void User::setTags(const QList<QVariant> &tags)
     m_tags = tagsStrings;
 }
 
+void User::setTags(const QString &tags)
+{
+    QHash<QString, QString> tagsStrings;
+    QStringList invTags = tags.split(",");
+
+    for (int i = 0; i < invTags.count(); ++i)
+        tagsStrings.insert(QString::number(i), invTags.at(i));
+
+    m_tags = tagsStrings;
+}
+
 void User::setTimestamp(const QString &timestamp)
 {
     m_timestamp = timestamp;
index 3b2773c..7dbae2b 100644 (file)
@@ -122,6 +122,13 @@ public:
     void setTags(const QList<QVariant> &tags);
 
     /**
+    * @brief Sets user's tags.
+    *
+    * @param tags user's tags
+    */
+    void setTags(const QString &tags);
+
+    /**
     * @brief Set timestamp for last status update, timestamp is in literal mode
     *
     * @param timestamp timestamp