Copyright (C) 2010 Ixonos Plc. Authors:
Henri Lampela - henri.lampela@ixonos.com
+ Jussi Laitinen - jussi.laitinen@ixonos.com
+ Sami Rämö - sami.ramo@ixonos.com
Situare is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
#define SITUARESERVICE_H
#include <QObject>
-#include "../facebookservice/facebookcredentials.h"
+
#include "../user/user.h"
#include "imagefetcher.h"
+#include "message.h"
+class Database;
class NetworkAccessManager;
class QNetworkReply;
class QNetworkRequest;
* @brief SituareService class for communicating with Situare server
*
* @author Henri Lampela
-* @class SituareService situareservice.h "situareservice/situareservice.h"
+* @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
+* @author Sami Rämö - sami.ramo (at) ixonos.com
*/
class SituareService : public QObject
{
Q_OBJECT
+private:
+ /**
+ * @brief Request name enum
+ */
+ enum RequestName { RequestUnknown,
+ RequestGetLocations,
+ RequestUpdateLocation,
+ RequestReverseGeo};
+
public:
/**
+ * Unit test class
+ */
+ friend class TestSituareService;
+
+ enum SuccessfulMethod {SuccessfulUpdateLocation, SuccessfulAddTags, SuccessfulRemoveTags,
+ SuccessfulSendMessage, SuccessfulRemoveMessage};
+
+ /**
* @brief Default constructor
*
* @param parent instance of parent
*/
- SituareService(QObject *parent = 0);
+ SituareService(NetworkAccessManager *networkManager, ImageFetcher *imageFetcher,
+ QObject *parent = 0);
/**
* @brief Destructor
/*******************************************************************************
* MEMBER FUNCTIONS AND SLOTS
******************************************************************************/
+ /**
+ * @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
+ */
+ void fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
+ const GeoCoordinate &northEastCoordinates);
/**
* @brief Retrieves location user and friends information from Situare server
void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
public slots:
+ /**
+ * @brief Adds tags to the Situare server
+ *
+ * CURRENTLY TAGS ARE UPDATED TO THE LOCAL DATABASE, NOT SITUARE SERVER
+ * @param tags list of user's tags
+ */
+ void addTags(const QStringList &tags);
/**
* @brief Public slot, to clear user data
void clearUserData();
/**
- * @brief Public slot, which indicates when facebook credentials are ready
+ * @brief Retrieves messages sent to user.
+ */
+ void fetchMessages();
+
+ /**
+ * @brief Retrieves popular tags.
*
- * @param credentials New credentials
+ * Tags are fetch from local database instead of Situare server.
*/
- void credentialsReady(const FacebookCredentials &credentials);
+ void fetchPopularTags();
+
+ /**
+ * @brief Removes message.
+ *
+ * @param id message ID
+ */
+ void removeMessage(const QString &id);
+
+ /**
+ * @brief Removes tags.
+ *
+ * @param tags list of tags to remove
+ */
+ void removeTags(const QStringList &tags);
/**
* @brief Public slot, which indicates when http request has been completed
*/
void requestFinished(QNetworkReply *reply);
- void updateAccessToken(const QString &accessToken);
+ /**
+ * @brief Searches people by tag name.
+ *
+ * @param tag tag name
+ */
+ void searchPeopleByTag(const QString &tag);
-private:
+ /**
+ * @brief Sends a message to a person.
+ *
+ * @param receiverId Facebook user ID
+ * @param message message text
+ * @param message coordinates
+ */
+ void sendMessage(const QString &receiverId, const QString &message,
+ const GeoCoordinate &coordinates = GeoCoordinate());
/**
+ * @brief Update session data
+ *
+ * @param session New session data
+ */
+ void updateSession(const QString &session);
+
+private:
+ /**
* @brief Requests ImageFetcher if user/friend has a profile image
* uses members: m_user and m_friendsList
*
* @param imageUrlList list of image urls
*/
- void addProfileImages(const QList<QUrl> &imageUrlList);
+ void addProfileImages(const QHash<QString, QUrl> &imageUrlList);
+ /**
+ * @brief Append access_token and other session data to the reques url
+ *
+ * @param[in,out] requestUrl Request URL with other request parameters and ending to &
+ */
void appendAccessToken(QString &requestUrl);
/**
- * @brief Forms a http cookie
+ * @brief Build and send request
+ *
+ * Appends script pathname and parameters to the server base URL. Access token is appended and
+ * the request sent if the access token is available, otherwise the request is queued.
+ *
+ * @param script Requested script pathname
+ * @param parameters Hash containing parameter key/value pairs.
+ */
+ void buildRequest(const QString &script, const QHash<QString,QString> ¶meters);
+
+ /**
+ * @brief Convert coordinate value in degrees (double) to string with enough precision
+ *
+ * @param degrees Coordinate value in degrees
+ * @returns Coordinate value as string
+ */
+ QString degreesToString(double degrees);
+
+ /**
+ * @brief Returns reuquest script's name.
*
- * @param apiKeyValue application key
- * @param expiresValue session expire date&time from Facebook
- * @param userValue user id from Facebook
- * @param sessionKeyValue session key from Facebook
- * @param sessionSecretValue session secret from Facebook
- * @param signatureValue md5 generated signature
- * @param localeValue used locale
- * @return QString formed cookie
+ * @param url url to check
+ * @return RequestName
*/
- QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
- QString sessionKeyValue, QString sessionSecretValue,
- const QString &signatureValue, const QString &localeValue);
+ SituareService::RequestName getRequestName(const QUrl &url) const;
/**
- * @brief Forms a http url
+ * @brief Temporary method to get tags.
*
- * @param baseUrl Server url
- * @param phpScript Server script
- * @param urlParameters optional parameters for url
- * @return QUrl formed url
+ * Tags are fetch from local database instead of Situare server.
+ * @param userId
+ * @return QStringList list of tags
*/
- QUrl formUrl(const QString &baseUrl, const QString &phpScript,
- QString urlParameters = QString());
+ QHash<QString, QString> getTags(const QString &userId);
/**
- * @brief Forms url parameters
+ * @brief Parses interesting people data from JSON string
*
- * @param coordinates current coordinates
- * @param status optional status message
- * @param publish optional publish location on Facebook wall
- * @return QString
+ * @param jsonReply JSON string
*/
- QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
- bool publish = false);
+ void parseInterestingPeopleData(const QByteArray &jsonReply);
/**
- * @brief Parses user and friend data from JSON string
+ * @brief Parses messages data from JSON string
*
* @param jsonReply JSON string
*/
- void parseUserData(const QByteArray &jsonReply);
+ void parseMessagesData(const QByteArray &jsonReply);
/**
- * @brief Sends http request
+ * @brief Parses popular tags data from JSON string
*
- * @param url destination
- * @param cookieType type of the cookie
- * @param cookie http cookie
+ * @param jsonReply JSON string
*/
- void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
+ void parsePopularTagsData(const QByteArray &jsonReply);
+ /**
+ * @brief Parses reply from JSON string
+ *
+ * Calls different parse methods or emits error signal if response contains error status.
+ */
+ void parseReply(const QByteArray &jsonReply, RequestName requestName);
- void buildRequest(const QString &script, const QHash<QString,QString> ¶meters);
+ /**
+ * @brief Parses reverse geo data
+ *
+ * @param reverseGeoData reverse geo data QVariant tree
+ */
+ void parseReverseGeoData(const QVariant &reverseGeoData);
+ /**
+ * @brief Parses user and friend data from JSON string
+ *
+ * @param userData user data QVariant tree
+ */
+ void parseUserData(const QVariant &userData);
+
+ /**
+ * @brief Send request
+ *
+ * @param requestUrl Request URL containing also all parameters
+ */
void sendRequest(const QString &requestUrl);
private slots:
-
/**
* @brief Slot for received images
*
- * @param url Image url
- * @param image Received image
+ * @param id image ID
+ * @param image image pixmap
*/
- void imageReceived(const QUrl &url, const QPixmap &image);
+ void imageReceived(const QString &id, const QPixmap &image);
/*******************************************************************************
* SIGNALS
******************************************************************************/
-
signals:
-
/**
* @brief Signals error
*
/**
* @brief Signal for image fetching
*
+ * @param id Image id
* @param url Image url
*/
- void fetchImage(const QUrl &url);
+ void fetchImage(const QString &id, const QUrl &url);
+
+ /**
+ * @brief Signals when image is downloaded
+ *
+ * @param id image ID
+ * @param image image pixmap
+ */
+ void imageReady(const QString &id, const QPixmap &image);
/**
- * @brief Signals when user's/friend's image is downloaded
+ * @brief Signal when fetchPeopleWithSimilarInterest request is finished
*
- * @param user Instance of user/friend
+ * @param friends list of friends
+ * @param others list of other people
*/
- void imageReady(User *user);
+ void interestingPeopleReceived(QList<User> &friends, QList<User> &others);
+
+ /**
+ * @brief Signal when fetchMessages request is finished
+ *
+ * @param messages list of messages sent to user
+ */
+ void messagesReceived(QList<Message> &received, QList<Message> &sent);
+
+ /**
+ * @brief Signals when fetchPopularTags request is finished
+ *
+ * @param popularTags list of popular tags
+ */
+ void popularTagsReceived(QHash<QString, QString> &popularTags);
/**
* @brief Signals when address data is retrieved
/**
* @brief Signals when updateLocation request finished successfully
- *
*/
- void updateWasSuccessful();
+ void updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod);
/**
* @brief Signals when user data is retrieved
/*******************************************************************************
* DATA MEMBERS
******************************************************************************/
-
private:
-
bool m_defaultImage; ///< Indicates if some of the friends/user doesn't have a image
- QList<QNetworkReply *> m_currentRequests; ///< List of current http requests
- QList<QString> m_requestsWaitingAccessToken;
- QList<User *> m_friendsList; ///< List of friends(User)
-
- QString m_accessToken;
+ QList<QNetworkReply *> m_currentRequests; ///< List of current http requests
+ QList<User *> m_friendsList; ///< List of friends(User)
- NetworkAccessManager *m_networkManager; ///< Pointer to QNetworkAccessManager
+ QString m_session; ///< Session data
- FacebookCredentials m_credentials; ///< handle for FacebookCredentials
- ImageFetcher *m_imageFetcher; ///< Instance of the image fetcher
+ Database *m_database; ///< Instance of the database
+ ImageFetcher *m_imageFetcher; ///< Instance of the image fetcher
+ NetworkAccessManager *m_networkManager; ///< Pointer to QNetworkAccessManager
User *m_user; ///< Pointer to User
};