USA.
*/
+#include "parser.h"
+
#include <QtAlgorithms>
#include <QDebug>
-#include <QtGlobal>
-#include <QStringList>
+#include <QtNetwork/QNetworkReply>
#include <QPixmap>
-#include <QNetworkReply>
-#include "situareservice.h"
+#include <QStringList>
+#include <QtGlobal>
+
+#include "database.h"
+#include "error.h"
+#include "network/networkaccessmanager.h"
#include "situarecommon.h"
-#include "parser.h"
#include "ui/avatarimage.h"
-SituareService::SituareService(QObject *parent)
+#include "situareservice.h"
+
+SituareService::SituareService(NetworkAccessManager *networkManager, ImageFetcher *imageFetcher,
+ QObject *parent)
: QObject(parent),
m_user(0)
{
qDebug() << __PRETTY_FUNCTION__;
- m_networkManager = new QNetworkAccessManager;
- connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
-
- m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
- 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_networkManager = networkManager;
+ connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
+ this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
+
+ m_imageFetcher = imageFetcher;
+ connect(this, SIGNAL(fetchImage(QString, QUrl)),
+ m_imageFetcher, SLOT(fetchImage(QString, QUrl)));
+ connect(m_imageFetcher, SIGNAL(imageReceived(QString,QPixmap)),
+ this, SLOT(imageReceived(QString,QPixmap)));
+ connect(m_imageFetcher, SIGNAL(error(int, int)),
+ this, SIGNAL(error(int, int)));
+
+ m_database = new Database(this);
+ m_database->openDatabase();
+ m_database->createNotificationTable();
+ m_database->createUserTable();
+ m_database->createTagTable();
+ m_database->createUserTagTable();
}
SituareService::~SituareService()
{
qDebug() << __PRETTY_FUNCTION__;
- delete m_networkManager;
- m_networkManager = 0;
if(m_user) {
delete m_user;
m_user = 0;
}
- delete m_imageFetcher;
- m_imageFetcher = 0;
qDeleteAll(m_friendsList.begin(), m_friendsList.end());
m_friendsList.clear();
}
+void SituareService::fetchMessages()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ //Request sent to server does not need the UID
+ QByteArray arr = m_database->getNotifications(613374451);
+
+ parseMessagesData(arr);
+}
+
+void SituareService::fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
+ const GeoCoordinate &northEastCoordinates)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ //Request sent to server does not need the UID
+ QByteArray arr = m_database->getInterestingPeople(613374451,
+ southWestCoordinates,
+ northEastCoordinates);
+
+ parseInterestingPeopleData(arr);
+}
+
+void SituareService::fetchPopularTags()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QByteArray arr = m_database->getPopularTags();
+
+ parsePopularTagsData(arr);
+}
+
void SituareService::fetchLocations()
{
qDebug() << __PRETTY_FUNCTION__;
sendRequest(url, COOKIE, cookie);
}
-void SituareService::reverseGeo(const QPointF &coordinates)
+void SituareService::reverseGeo(const GeoCoordinate &coordinates)
{
qDebug() << __PRETTY_FUNCTION__;
m_credentials.sig(), EN_LOCALE);
QString urlParameters = formUrlParameters(coordinates);
+ urlParameters.append(JSON_FORMAT);
QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
sendRequest(url, COOKIE, cookie);
}
-void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
+void SituareService::updateLocation(const GeoCoordinate &coordinates, const QString &status,
const bool &publish)
{
qDebug() << __PRETTY_FUNCTION__;
+ QString urlParameters = formUrlParameters(coordinates, status, publish);
+ QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
+
QString cookie = formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
m_credentials.sessionKey(), m_credentials.sessionSecret(),
m_credentials.sig(), EN_LOCALE);
-
- QString publishValue;
- if(publish) {
- publishValue = PUBLISH_TRUE;
- }
- else {
- publishValue = PUBLISH_FALSE;
- }
- QString urlParameters = formUrlParameters(coordinates, status, publishValue);
- QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
-
sendRequest(url, COOKIE, cookie);
}
return url;
}
-QString SituareService::formUrlParameters(const QPointF &coordinates, QString status,
- QString publish)
+QString SituareService::formUrlParameters(const GeoCoordinate &coordinates, QString status,
+ bool publish)
{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ // one scene pixel is about 5.4e-6 degrees, the integer part is max three digits and one
+ // additional digit is added for maximum precision
+ const int COORDINATE_PRECISION = 10;
+
QString parameters;
parameters.append(QUESTION_MARK);
parameters.append(LATITUDE);
parameters.append(EQUAL_MARK);
- parameters.append(QString::number(coordinates.y()));
+ parameters.append(QString::number(coordinates.latitude(), 'f', COORDINATE_PRECISION));
parameters.append(AMBERSAND_MARK);
parameters.append(LONGTITUDE);
parameters.append(EQUAL_MARK);
- parameters.append(QString::number(coordinates.x()));
+ parameters.append(QString::number(coordinates.longitude(), 'f', COORDINATE_PRECISION));
- if(publish.compare(PUBLISH_TRUE) == 0) {
- parameters.append(AMBERSAND_MARK);
- parameters.append(PUBLISH);
- parameters.append(EQUAL_MARK);
+ parameters.append(AMBERSAND_MARK);
+ parameters.append(PUBLISH);
+ parameters.append(EQUAL_MARK);
+
+ if(publish)
parameters.append(PUBLISH_TRUE);
- }
- else if(publish.compare(PUBLISH_FALSE) == 0) {
- parameters.append(AMBERSAND_MARK);
- parameters.append(PUBLISH);
- parameters.append(EQUAL_MARK);
+ else
parameters.append(PUBLISH_FALSE);
- }
if(!status.isEmpty()) {
parameters.append(AMBERSAND_MARK);
return parameters;
}
+void SituareService::sendMessage(const QString &receiverId, const QString &message,
+ const GeoCoordinate &coordinates)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_database->sendMessage(613374451, receiverId.toULongLong(), message, coordinates))
+ emit updateWasSuccessful(SituareService::SuccessfulSendMessage);
+}
+
void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
{
- qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
+ qDebug() << __PRETTY_FUNCTION__;
QNetworkRequest request;
request.setUrl(url);
+ request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
- QNetworkReply *reply = m_networkManager->get(request);
+ QNetworkReply *reply = m_networkManager->get(request, true);
m_currentRequests.append(reply);
}
{
qDebug() << __PRETTY_FUNCTION__;
- QUrl url = reply->url();
- qDebug() << "BytesAvailable: " << reply->bytesAvailable();
- if (reply->error()) {
- qDebug() << reply->errorString();
- emit error(reply->errorString());
- }
- else {
- qint64 max = reply->size();
- QByteArray replyArray = reply->read(max);
- qDebug() << "Reply from: " << url << "reply " << replyArray;
- // ToDo: results handling includes Situare's errors
- // works like situare's error handling i.e. both lat & lon are missing/wrong
- // -> we get only error for wrong lon
- if(replyArray == ERROR_LAT.toAscii()) {
- qDebug() << "Error: " << ERROR_LAT;
- emit error(replyArray);
- }
- else if(replyArray == ERROR_LON.toAscii()) {
- qDebug() << "Error: " << ERROR_LON;
- emit error(replyArray);
+ //Reply from situare
+ if (m_currentRequests.contains(reply)) {
+
+ qDebug() << "BytesAvailable: " << reply->bytesAvailable();
+
+ if (reply->error()) {
+ emit error(ErrorContext::NETWORK, reply->error());
+ } else {
+ QByteArray replyArray = reply->readAll();
+ qDebug() << "Reply from: " << reply->url() << "reply " << replyArray;
+
+ if(replyArray == ERROR_LAT.toAscii()) {
+ qDebug() << "Error: " << ERROR_LAT;
+ emit error(ErrorContext::SITUARE, SituareError::UPDATE_FAILED);
+ } else if(replyArray == ERROR_LON.toAscii()) {
+ qDebug() << "Error: " << ERROR_LON;
+ emit error(ErrorContext::SITUARE, SituareError::UPDATE_FAILED);
+ } else if(replyArray.contains(ERROR_SESSION.toAscii())) {
+ qDebug() << "Error: " << ERROR_SESSION;
+ emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
+ } else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
+ qDebug() << "JSON string";
+ parseUserData(replyArray);
+ } else if(replyArray.isEmpty()) {
+ if(reply->url().toString().contains(UPDATE_LOCATION.toAscii())) {
+ emit updateWasSuccessful(SituareService::SuccessfulUpdateLocation);
+ } else {
+ // session credentials are invalid
+ emit error(ErrorContext::SITUARE, SituareError::SESSION_EXPIRED);
+ }
+ } else {
+ // unknown reply
+ emit error(ErrorContext::SITUARE, SituareError::ERROR_GENERAL);
+ }
}
- else if(replyArray.contains(ERROR_SESSION.toAscii())) {
- qDebug() << "Error: " << ERROR_SESSION;
- emit invalidSessionCredentials();
+ m_currentRequests.removeAll(reply);
+ reply->deleteLater();
+ }
+}
+
+void SituareService::credentialsReady(const FacebookCredentials &credentials)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_credentials = credentials;
+}
+
+void SituareService::parseInterestingPeopleData(const QByteArray &jsonReply)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QJson::Parser parser;
+ bool ok;
+
+ QVariantMap result = parser.parse(jsonReply, &ok).toMap();
+
+ if (!ok) {
+ emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
+ return;
+ } else {
+ QVariant people = result["people"];
+
+ QList<User> friends;
+ QList<User> others;
+
+ 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));
+
+ friends.append(user);
+
+ //Remove comment when the actual server is used
+ //emit fetchImage(user.userId(), user.profileImageUrl());
}
- else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
- qDebug() << "JSON string";
- parseUserData(replyArray);
+
+ 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());
+
+ others.append(user);
+
+ //Remove comment when the actual server is used
+ //emit fetchImage(user.userId(), user.profileImageUrl());
}
- else if(replyArray == "") {
- if(url.toString().contains(UPDATE_LOCATION.toAscii())) {
- emit updateWasSuccessful();
- }
- else {
- // session credentials are invalid
- emit invalidSessionCredentials();
+
+ emit interestingPeopleReceived(friends, others);
+ }
+}
+
+void SituareService::parseMessagesData(const QByteArray &jsonReply)
+{
+ QJson::Parser parser;
+ bool ok;
+
+ QVariantMap result = parser.parse(jsonReply, &ok).toMap();
+
+ if (!ok) {
+ emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
+ return;
+ } else {
+ QVariant messages = result["messages"];
+
+ QList<Message> received;
+ QList<Message> sent;
+
+ foreach (QVariant messageVariant, messages.toMap().value("received").toList()) {
+ Message message(Message::MessageTypeReceived);
+ QMap<QString, QVariant> messageMap = messageVariant.toMap();
+ message.setId(messageMap["id"].toString());
+ message.setSenderId(messageMap["sender_id"].toString());
+ message.setReceiverId(messageMap["receiver_id"].toString());
+ message.setSenderName(messageMap["sender_name"].toString());
+ uint timestampSeconds = messageMap["timestamp"].toUInt();
+ message.setTimestamp(QDateTime::fromTime_t(timestampSeconds));
+ message.setText(messageMap["text"].toString());
+ message.setImage(AvatarImage::create(
+ QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
+
+ bool latOk;
+ qreal latitude = messageMap["latitude"].toReal(&latOk);
+ bool lonOk;
+ qreal longitude = messageMap["longitude"].toReal(&lonOk);
+
+ if (latOk && lonOk) {
+ message.setAddress(messageMap["address"].toString());
+ message.setCoordinates(GeoCoordinate(latitude, longitude));
}
+
+ received.append(message);
+
+ //emit fetchImage(message.id(), messageMap["image_url"].toString());
}
- else {
- // Street address ready
- QString address = QString::fromUtf8(replyArray);
- emit reverseGeoReady(address);
+
+ foreach (QVariant messageVariant, messages.toMap().value("sent").toList()) {
+ Message message(Message::MessageTypeSent);
+ QMap<QString, QVariant> messageMap = messageVariant.toMap();
+ message.setId(messageMap["id"].toString());
+ message.setSenderId(messageMap["sender_id"].toString());
+ message.setReceiverId(messageMap["receiver_id"].toString());
+ message.setSenderName(messageMap["sender_name"].toString());
+ uint timestampSeconds = messageMap["timestamp"].toUInt();
+ message.setTimestamp(QDateTime::fromTime_t(timestampSeconds));
+ message.setText(messageMap["text"].toString());
+ message.setImage(AvatarImage::create(
+ QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
+
+ bool latOk;
+ qreal latitude = messageMap["latitude"].toReal(&latOk);
+ bool lonOk;
+ qreal longitude = messageMap["longitude"].toReal(&lonOk);
+
+ if (latOk && lonOk) {
+ message.setAddress(messageMap["address"].toString());
+ message.setCoordinates(GeoCoordinate(latitude, longitude));
+ }
+
+ sent.append(message);
+
+ //emit fetchImage(message.id(), messageMap["image_url"].toString());
}
+
+ emit messagesReceived(received, sent);
}
- m_currentRequests.removeAll(reply);
- reply->deleteLater();
}
-void SituareService::credentialsReady(const FacebookCredentials &credentials)
+void SituareService::parsePopularTagsData(const QByteArray &jsonReply)
{
qDebug() << __PRETTY_FUNCTION__;
- m_credentials = credentials;
+ QJson::Parser parser;
+ bool ok;
+
+ QVariantMap result = parser.parse(jsonReply, &ok).toMap();
+
+ if (!ok) {
+ emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
+ return;
+ } else {
+ QHash<QString, QString> popularTags;
+
+ foreach (QVariant tagVariant, result["popular_tags"].toList()) {
+ QMap<QString, QVariant> tag = tagVariant.toMap();
+ popularTags.insert(tag["id"].toString(), tag["name"].toString());
+ }
+
+ emit interestingPeopleReceived(friends, others);
+ }
}
+
void SituareService::parseUserData(const QByteArray &jsonReply)
{
qDebug() << __PRETTY_FUNCTION__;
- m_visited = 0;
- m_nbrOfImages = 0;
m_defaultImage = false;
- qDeleteAll(m_friendsList.begin(), m_friendsList.end());
- m_friendsList.clear();
-
- if(m_user) {
- delete m_user;
- m_user = 0;
- }
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse (jsonReply, &ok).toMap();
+
if (!ok) {
+ emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
+ return;
+ } else {
- qFatal("An error occurred during parsing");
- exit (1);
- }
+ 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();
+ QVariant userVariant = result.value("user");
+ QMap<QString, QVariant> userMap = userVariant.toMap();
- QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
+ GeoCoordinate coordinates(userMap["latitude"].toReal(), userMap["longitude"].toReal());
- QUrl imageUrl = userMap["pic_big"].toUrl();
- qWarning() << "IMAGE URL: " << imageUrl;
+ QUrl imageUrl = userMap[NORMAL_SIZE_PROFILE_IMAGE].toUrl();
+ 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(", ");
+ }
- if(imageUrl.isEmpty()) {
- // user doesn't have profile image, so we need to get him a silhouette image
- m_defaultImage = true;
- }
+ User user = User(address, coordinates, userMap["name"].toString(),
+ userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
+ true, userMap["uid"].toString());
- m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
- userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
- true, userMap["uid"].toString());
+ if(imageUrl.isEmpty()) {
+ // user doesn't have profile image, so we need to get him a silhouette image
+ m_defaultImage = true;
+ user.setProfileImage(AvatarImage::create(
+ QPixmap(":/res/images/empty_avatar_big.png"), AvatarImage::Large));
+ }
- foreach (QVariant friendsVariant, result["friends"].toList()) {
- QMap<QString, QVariant> friendMap = friendsVariant.toMap();
- QVariant distance = friendMap["distance"];
- QMap<QString, QVariant> distanceMap = distance.toMap();
+ QList<User> tmpFriendsList;
- QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
+ foreach (QVariant friendsVariant, result["friends"].toList()) {
+ QMap<QString, QVariant> friendMap = friendsVariant.toMap();
+ QVariant distance = friendMap["distance"];
+ QMap<QString, QVariant> distanceMap = distance.toMap();
- QUrl imageUrl = friendMap["profile_pic"].toUrl();
+ GeoCoordinate coordinates(friendMap["latitude"].toReal(),friendMap["longitude"].toReal());
- if(imageUrl.isEmpty()) {
- // friend doesn't have profile image, so we need to get him a silhouette image
- m_defaultImage = true;
- }
+ QUrl imageUrl = friendMap["profile_pic"].toUrl();
- 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(),
- distanceMap["value"].toDouble());
+ 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(", ");
+ }
- m_friendsList.append(user);
- }
- addProfileImages();
-}
+ 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());
-void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
-{
- qDebug() << __PRETTY_FUNCTION__;
- qDebug() << "Image URL: " << url << " size :" << image.size();
+ if(imageUrl.isEmpty()) {
+ // friend doesn't have profile image, so we need to get him a silhouette image
+ m_defaultImage = true;
+ buddy.setProfileImage(AvatarImage::create(
+ QPixmap(":/res/images/empty_avatar.png"), AvatarImage::Small));
+ }
- // 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(AvatarImage::create(QPixmap(
- ":res/images/big_profile_image.gif")));
- }
- for(int i=0;i < m_friendsList.count();i++) {
- if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
- m_friendsList.at(i)->setProfileImage(AvatarImage::create(image));
+ tmpFriendsList.append(buddy);
}
- }
- }
- if (m_user->profileImageUrl() == url) {
- m_user->setProfileImage(AvatarImage::create(image));
- }
+ QHash<QString, 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.insert(user.userId(), user.profileImageUrl());
+ } else {
+ user.setProfileImage(m_user->profileImage());
+ }
+ } else {
+ if(!user.profileImageUrl().isEmpty())
+ imageUrlList.insert(user.userId(), user.profileImageUrl());
+ }
- for(int i=0;i<m_friendsList.count();i++) {
- if(m_friendsList.at(i)->profileImageUrl() == url) {
- m_friendsList.at(i)->setProfileImage(AvatarImage::create(image));
- m_nbrOfImages++; // indicates how many friend profile images has been downloaded
- }
- }
+ // 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.insert(tmpBuddy.userId(), tmpBuddy.profileImageUrl());
+ }
+ }
+ } else {
+ foreach(User buddy, tmpFriendsList) {
+ if(!buddy.profileImageUrl().isEmpty())
+ imageUrlList.insert(buddy.userId(), buddy.profileImageUrl());
+ }
+ }
- if(m_nbrOfImages == m_visited) {
- qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
- qDebug() << "emit userDataChanged";
- emit userDataChanged(m_user, m_friendsList);
+ // 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();
+
+ //REMOVE WHEN NOT NEEDED! get user tags and set tags to the user
+ m_user->setTags(getTags(m_user->userId()));
+
+ emit userDataChanged(m_user, m_friendsList);
+
+ // set silhouette image to imageUrlList for downloading
+ if(m_defaultImage)
+ imageUrlList.insert("", 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(", "));
+ }
+ }
}
}
-void SituareService::addProfileImages()
+void SituareService::imageReceived(const QString &id, const QPixmap &image)
{
qDebug() << __PRETTY_FUNCTION__;
- // reduce net traffic by sending only one download request for facebook silhouette image
- if(m_defaultImage) {
- emit fetchImage(QUrl(SILHOUETTE_URL));
- }
+ if (m_user->userId() == id)
+ emit imageReady(id, AvatarImage::create(image, AvatarImage::Large));
+ else
+ emit imageReady(id, AvatarImage::create(image, AvatarImage::Small));
+}
+
+void SituareService::addProfileImages(const QHash<QString, QUrl> &imageUrlList)
+{
+ qDebug() << __PRETTY_FUNCTION__;
- if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid())
- emit fetchImage(m_user->profileImageUrl());
+ QHashIterator<QString, QUrl> imageUrlListIterator(imageUrlList);
- for(int i=0;i<m_friendsList.count();i++) {
- 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());
- }
+ while (imageUrlListIterator.hasNext()) {
+ imageUrlListIterator.next();
+ emit fetchImage(imageUrlListIterator.key(), imageUrlListIterator.value());
}
}
}
emit userDataChanged(m_user, m_friendsList);
}
+
+QHash<QString, QString> SituareService::getTags(const QString &userId)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ return m_database->getTags(userId.toInt());
+}
+
+void SituareService::removeTags(const QStringList &tags)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_database->removeTags(613374451, tags))
+ emit updateWasSuccessful(SituareService::SuccessfulRemoveTags);
+}
+
+void SituareService::removeMessage(const QString &id)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_database->removeMessage(613374451, id))
+ emit updateWasSuccessful(SituareService::SuccessfulRemoveMessage);
+}
+
+void SituareService::addTags(const QStringList &tags)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ foreach (QString tag, tags)
+ m_database->addTag(613374451, tag);
+
+ emit updateWasSuccessful(SituareService::SuccessfulAddTags);
+}
+
+void SituareService::searchPeopleByTag(const QString &tag)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QByteArray arr = m_database->getInterestingPeopleByTag(613374451, tag);
+
+ parseInterestingPeopleData(arr);
+}