+void SituareService::parseUserData(const QByteArray &jsonReply)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ qWarning() << __PRETTY_FUNCTION__ << "Server reply:" << jsonReply;
+
+ m_defaultImage = false;
+
+ QJson::Parser parser;
+ bool ok;
+
+ QVariantMap result = parser.parse (jsonReply, &ok).toMap();
+ if (!ok) {
+ emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
+ return;
+ } else {
+
+ if(result.contains("ErrorCode")) {
+ QVariant errorVariant = result.value("ErrorCode");
+ emit error(ErrorContext::SITUARE, errorVariant.toInt());
+ return;
+ } else if(result.contains("user")) {
+
+ QVariant userVariant = result.value("user");
+ QMap<QString, QVariant> userMap = userVariant.toMap();
+
+ GeoCoordinate coordinates(userMap["latitude"].toReal(), userMap["longitude"].toReal());
+
+ QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
+
+ if(imageUrl.isEmpty()) {
+ // user doesn't have profile image, so we need to get him a silhouette image
+ m_defaultImage = true;
+ }
+
+ QString address = userMap["address"].toString();
+ if(address.isEmpty()) {
+ QStringList location;
+ location.append(QString::number(coordinates.latitude()));
+ location.append(QString::number(coordinates.longitude()));
+ address = location.join(", ");
+ }
+
+ User user = User(address, coordinates, userMap["name"].toString(),
+ userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
+ true, userMap["uid"].toString());
+
+ QList<User> tmpFriendsList;
+
+ foreach (QVariant friendsVariant, result["friends"].toList()) {
+ QMap<QString, QVariant> friendMap = friendsVariant.toMap();
+ QVariant distance = friendMap["distance"];
+ QMap<QString, QVariant> distanceMap = distance.toMap();
+
+ GeoCoordinate coordinates(friendMap["latitude"].toReal(),friendMap["longitude"].toReal());
+
+ 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;
+ }
+
+ QString address = friendMap["address"].toString();
+ if(address.isEmpty()) {
+ QStringList location;
+ location.append(QString::number(coordinates.latitude()));
+ location.append(QString::number(coordinates.longitude()));
+ address = location.join(", ");
+ }
+
+ User buddy = User(address, coordinates, friendMap["name"].toString(),
+ friendMap["note"].toString(), imageUrl,
+ friendMap["timestamp"].toString(),
+ false, friendMap["uid"].toString(), distanceMap["units"].toString(),
+ distanceMap["value"].toDouble());
+
+ tmpFriendsList.append(buddy);
+ }
+
+ QList<QUrl> imageUrlList; // url list for images
+
+ // set unchanged profile images or add new images to imageUrlList for downloading
+ if(m_user) {
+ if(m_user->profileImageUrl() != user.profileImageUrl()) {
+ if(!user.profileImageUrl().isEmpty())
+ imageUrlList.append(user.profileImageUrl());
+ } else {
+ user.setProfileImage(m_user->profileImage());
+ }
+ } else {
+ if(!user.profileImageUrl().isEmpty())
+ imageUrlList.append(user.profileImageUrl());
+ }
+
+ // clear old user object
+ if(m_user) {
+ delete m_user;
+ m_user = 0;
+ }
+
+ // create new user object from temporary user object
+ m_user = new User(user);
+
+ // set unchanged profile images or add new images to imageUrlList for downloading
+ if(!m_friendsList.isEmpty()) {
+ foreach(User tmpBuddy, tmpFriendsList) {
+ if(!tmpBuddy.profileImageUrl().isEmpty()) {
+ bool found = false;
+ foreach(User *buddy, m_friendsList) {
+ if(tmpBuddy.profileImageUrl() == buddy->profileImageUrl()) {
+ tmpBuddy.setProfileImage(buddy->profileImage());
+ found = true;
+ break;
+ }
+ }
+ if(!found && !tmpBuddy.profileImageUrl().isEmpty())
+ imageUrlList.append(tmpBuddy.profileImageUrl());
+ }
+ }
+ } else {
+ foreach(User buddy, tmpFriendsList) {
+ if(!buddy.profileImageUrl().isEmpty())
+ imageUrlList.append(buddy.profileImageUrl());
+ }
+ }
+
+ // clear old friendlist
+ qDeleteAll(m_friendsList.begin(), m_friendsList.end());
+ m_friendsList.clear();
+
+ // populate new friendlist with temporary friendlist's data
+ foreach(User tmpFriendItem, tmpFriendsList) {
+ User *friendItem = new User(tmpFriendItem);
+ m_friendsList.append(friendItem);
+ }
+ tmpFriendsList.clear();
+
+ emit userDataChanged(m_user, m_friendsList);
+
+ // set silhouette image to imageUrlList for downloading
+ if(m_defaultImage)
+ imageUrlList.append(QUrl(SILHOUETTE_URL));
+
+ addProfileImages(imageUrlList);
+ imageUrlList.clear();
+ } else {
+ QVariant address = result.value("address");
+ if(!address.toString().isEmpty()) {
+ emit reverseGeoReady(address.toString());
+ } else {
+ QStringList coordinates;
+ coordinates.append(result.value("lat").toString());
+ coordinates.append(result.value("lon").toString());
+
+ emit error(ErrorContext::SITUARE, SituareError::ADDRESS_RETRIEVAL_FAILED);
+ emit reverseGeoReady(coordinates.join(", "));
+ }
+ }
+ }