Updated user and friendslist signals and image fetching
authorlampehe-local <henri.lampela@ixonos.com>
Mon, 3 May 2010 12:09:00 +0000 (15:09 +0300)
committerlampehe-local <henri.lampela@ixonos.com>
Mon, 3 May 2010 12:09:00 +0000 (15:09 +0300)
src/engine/engine.cpp
src/engine/engine.h
src/situareservice/situarecommon.h
src/situareservice/situareservice.cpp
src/situareservice/situareservice.h

index 716f63a..1acbd4f 100644 (file)
@@ -45,6 +45,8 @@ SituareEngine::SituareEngine(QMainWindow *parent)
             this, SLOT(userDataChanged(User*,QList<User*>&)));
     connect(this, SIGNAL(userLocationReady(User*)), m_ui, SIGNAL(userLocationReady(User*)));
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)), m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
+       connect(m_situareService, SIGNAL(error(QString)), this, SLOT(error(QString)));
+    connect(m_situareService, SIGNAL(updateWasSuccessful()), this, SLOT(updateWasSuccessful()));
 
     m_facebookAuthenticator->start();
 }
@@ -56,6 +58,13 @@ SituareEngine::~SituareEngine()
     delete m_facebookAuthenticator;
 }
 
+void SituareEngine::error(const QString &error)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << error;
+    // ToDo: signal UI?
+}
+
 void SituareEngine::loginOk()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -67,27 +76,36 @@ void SituareEngine::loginOk()
 void SituareEngine::requestAddress()
 {
     qDebug() << __PRETTY_FUNCTION__;
-    QPointF coordinates(65, 25.5); // this will be get from somewhere, map etc...
+
+    QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
     m_situareService->reverseGeo(coordinates);
 }
 
 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    QPointF coordinates(65, 25.5); // this will be get from somewhere, map etc...
+
+    QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
     m_situareService->updateLocation(coordinates, status, publish);
 }
 
+void SituareEngine::updateWasSuccessful()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_situareService->fetchLocations();
+}
+
 void SituareEngine::updateFriendsList()
 {
     qDebug() << __PRETTY_FUNCTION__;
     //code here
 }
 
-void SituareEngine::userDataChanged(User *user, QList<User *> &friendList)
+void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
 {
     qDebug() << __PRETTY_FUNCTION__;
+
     emit userLocationReady(user);
-    qDebug() << friendList.at(0)->name();
-    emit friendsLocationsReady(friendList);
+    emit friendsLocationsReady(friendsList);
 }
index 03f1d88..c407233 100644 (file)
@@ -58,6 +58,14 @@ public:
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
 public slots:
+
+    /**
+    * @brief Slot to intercept error signal from ImageFetcher and SituareService
+    *
+    * @param error Error message
+    */
+    void error(const QString &error);
+
     /**
     * @brief Slot to intercept signal from successful login
     */
@@ -83,13 +91,38 @@ public slots:
     */
     void updateFriendsList();
 
-    void userDataChanged(User *user, QList<User *> &friendList);
+       /**
+    * @brief Slot to intercept signal from successful location update
+    *
+    */
+    void updateWasSuccessful();
+
+    /**
+    * @brief Slot to intercept signal when new user data is available.
+    *        Splits User and friendsList data and emits them as two different signals.
+    *
+    * @param user instance of User
+    * @param friendsList list of User instances (friends)
+    */
+    void userDataChanged(User *user, QList<User *> &friendsList);
 
 /*******************************************************************************
  * SIGNALS
  ******************************************************************************/
 signals:
+
+    /**
+    * @brief Signals when new user data is ready
+    *
+    * @param user Instance of User
+    */
     void userLocationReady(User *user);
+
+    /**
+    * @brief Signals when new friends data is ready
+    *
+    * @param friendList List of User instances (friends)
+    */
     void friendsLocationsReady(QList<User *> &friendList);
 
 /*******************************************************************************
index 09c2204..e60257b 100644 (file)
@@ -71,4 +71,6 @@ const QString ERROR_LAT = "Wrong lat value: . Latitude must be between -90 and 9
 const QString ERROR_LON = "Wrong lon value: . Longitude must be between -180 and 180!";
 const QString ERROR_SESSION = "<br />\n<b>Fatal error</b>:"; // starts with
 
+const QString SILHOUETTE_URL = "http://static.ak.fbcdn.net/pics/q_silhouette.gif";
+
 #endif // SITUARECOMMON_H
index 707ef51..744d404 100644 (file)
@@ -42,6 +42,7 @@ SituareService::SituareService(QObject *parent)
     connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
     connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
             SLOT(imageReceived(QUrl, QPixmap)));
+    connect(m_imageFetcher, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
 
     m_user = 0;
 }
@@ -86,6 +87,7 @@ void SituareService::reverseGeo(const QPointF &coordinates)
     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
                                               m_credentials.sig(), EN_LOCALE);
+
     QString urlParameters = formUrlParameters(coordinates);
     QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
 
@@ -193,7 +195,6 @@ void SituareService::requestFinished(QNetworkReply *reply)
     if (reply->error()) {
         qDebug() << reply->errorString();
         emit error(reply->errorString());
-        // ToDo: some general http error handling etc, signal UI?
     }
     else {
         qint64 max = reply->size();
@@ -204,17 +205,14 @@ void SituareService::requestFinished(QNetworkReply *reply)
         // -> we get only error for wrong lon
         if(replyArray == ERROR_LAT.toAscii()) {
             qDebug() << "Error: " << ERROR_LAT;
-            // ToDo: signal UI?
             emit error(replyArray);
         }
         else if(replyArray == ERROR_LON.toAscii()) {
             qDebug() << "Error: " << ERROR_LON;
-            // ToDo: signal UI?
             emit error(replyArray);
         }
         else if(replyArray.contains(ERROR_SESSION.toAscii())) {
             qDebug() << "Error: " << ERROR_SESSION;
-            // ToDo: signal UI?
             emit error(replyArray);
         }
         else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
@@ -223,6 +221,7 @@ void SituareService::requestFinished(QNetworkReply *reply)
         }
         else if(replyArray == "") {
             qDebug() << "No error, update was successful";
+            emit updateWasSuccessful();
         }
         else {
             // Street address ready
@@ -247,6 +246,7 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
 
     m_visited = 0;
     m_nbrOfImages = 0;
+    m_defaultImage = false;
     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
     m_friendsList.clear();
 
@@ -267,6 +267,11 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
 
     QUrl imageUrl = userMap["profile_pic"].toUrl();
 
+    if(imageUrl.isEmpty()) {
+        // user doesn't have profile image, so we need to get him a silhouette image
+        m_defaultImage = true;
+    }
+
     m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
                   userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
                   true, userMap["uid"].toString());
@@ -280,6 +285,11 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
 
       QUrl imageUrl = friendMap["profile_pic"].toUrl();
 
+      if(imageUrl.isEmpty()) {
+          // friend doesn't have profile image, so we need to get him a silhouette image
+          m_defaultImage = true;
+      }
+
       User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
                             friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
                             false, friendMap["uid"].toString(), distanceMap["units"].toString(),
@@ -295,6 +305,18 @@ void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
     qDebug() << __PRETTY_FUNCTION__;
     qDebug() << "Image URL: " << url << " size :" << image.size();
 
+    // assign facebook silhouette image to all who doesn't have a profile image
+    if(url == QUrl(SILHOUETTE_URL)) {
+        if(m_user->profileImageUrl().isEmpty()) {
+            m_user->setProfileImage(image);
+        }
+        for(int i=0;i < m_friendsList.count();i++) {
+            if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
+                m_friendsList.at(i)->setProfileImage(image);
+            }
+        }
+    }
+
     if(m_user->profileImageUrl() == url) {
         m_user->setProfileImage(image);
     }
@@ -317,12 +339,18 @@ void SituareService::addProfileImages()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(!m_user->profileImageUrl().isEmpty()) {
+    // reduce net traffic by sending only one download request for facebook silhouette image
+    if(m_defaultImage) {
+        emit fetchImage(QUrl(SILHOUETTE_URL));
+    }
+
+    if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
         emit fetchImage(m_user->profileImageUrl());
     }
 
     for(int i=0;i<m_friendsList.count();i++) {
-        if(!m_friendsList.at(i)->profileImageUrl().isEmpty()) {
+        if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
+           m_friendsList.at(i)->profileImageUrl().isValid()) {
             m_visited++; // indicates how many friends that have profile image
             emit fetchImage(m_friendsList.at(i)->profileImageUrl());
         }
index fde915e..605dfe9 100644 (file)
@@ -62,6 +62,7 @@ public:
 /*******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
+
     /**
     * @brief Retrieves location user and friends information from Situare server
     *
@@ -183,6 +184,12 @@ signals:
     void reverseGeoReady(const QString &address);
 
     /**
+    * @brief Signals when updateLocation request finished successfully
+    *
+    */
+    void updateWasSuccessful();
+
+    /**
     * @brief Signals when user data is retrieved
     *
     * @param user instance of user
@@ -205,6 +212,7 @@ private:
     QList<User *> m_friendsList; ///< List of friends(User)
     int m_visited; ///< Indicates number of friends with profile images
     int m_nbrOfImages; ///< Indicates number of friends whose profile images have been downloaded
+    bool m_defaultImage; ///< Indicates if some of the friends or the user does not have a profile image
 };
 
 #endif // SITUARESERVICE_H