Added error signals to engine from ImageFetcher and SituareService.
[situare] / src / situareservice / situareservice.cpp
index dd09f5a..6f86fbf 100644 (file)
@@ -41,6 +41,7 @@ SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
     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 = NULL;
 }
@@ -194,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();
@@ -205,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())) {
@@ -224,6 +221,7 @@ void SituareService::requestFinished(QNetworkReply *reply)
         }
         else if(replyArray == "") {
             qDebug() << "No error, update was successful";
+            // ToDo: signal UI?
         }
         else {
             // Street address ready
@@ -248,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();
 
@@ -268,6 +267,10 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
 
     QUrl imageUrl = userMap["profile_pic"].toUrl();
 
+    if(imageUrl.isEmpty()) {
+        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());
@@ -281,6 +284,10 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
 
       QUrl imageUrl = friendMap["profile_pic"].toUrl();
 
+      if(imageUrl.isEmpty()) {
+          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(),
@@ -296,6 +303,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);
     }
@@ -318,12 +337,17 @@ void SituareService::addProfileImages()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
+    // 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()) {
         emit fetchImage(m_user->profileImageUrl());
     }
 
     for(int i=0;i<m_friendsList.count();i++) {
-        if(!m_friendsList.at(i)->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
+        if(!m_friendsList.at(i)->profileImageUrl().isEmpty()) {
             m_visited++; // indicates how many friends that have profile image
             emit fetchImage(m_friendsList.at(i)->profileImageUrl());
         }