Added missing enum.
[situare] / src / situareservice / situareservice.h
index 11ab781..e7fb4a7 100644 (file)
@@ -3,6 +3,8 @@
    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;
-class QPointF;
+class GeoCoordinate;
 class QUrl;
 
 /**
 * @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
@@ -62,6 +85,15 @@ public:
 /*******************************************************************************
  * 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
@@ -74,18 +106,25 @@ public:
     *
     * @param coordinates coordinates to be translated
     */
-    void reverseGeo(const QPointF &coordinates);
+    void reverseGeo(const GeoCoordinate &coordinates);
 
     /**
     * @brief Updates location to the Situare server
     *
     * @param coordinates current cordinates
     * @param status message
-    * @param publish publish location on Facebook wall (true/false)
+    * @param publish publish location on Facebook wall
     */
-    void updateLocation(const QPointF &coordinates, const QString &status, const bool &publish);
+    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
@@ -94,11 +133,30 @@ public slots:
     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
@@ -107,85 +165,144 @@ public slots:
     */
     void requestFinished(QNetworkReply *reply);
 
-private:
+    /**
+    * @brief Searches people by tag name.
+    *
+    * @param tag tag name
+    */
+    void searchPeopleByTag(const QString &tag);
+
+    /**
+    * @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 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 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> &parameters);
+
+    /**
+      * @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 url url to check
+    * @return RequestName
     */
-    void addProfileImages();
+    SituareService::RequestName getRequestName(const QUrl &url) const;
 
     /**
-    * @brief Forms a http cookie
+    * @brief Temporary method to get tags.
     *
-    * @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
+    * Tags are fetch from local database instead of Situare server.
+    * @param userId
+    * @return QStringList list of tags
     */
-    QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
-                       QString sessionKeyValue, QString sessionSecretValue,
-                       const QString &signatureValue, const QString &localeValue);
+    QHash<QString, QString> getTags(const QString &userId);
 
     /**
-    * @brief Forms a http url
+    * @brief Parses interesting people data from JSON string
     *
-    * @param baseUrl Server url
-    * @param phpScript Server script
-    * @param urlParameters optional parameters for url
-    * @return QUrl formed url
+    * @param jsonReply JSON string
     */
-    QUrl formUrl(const QString &baseUrl, const QString &phpScript,
-                 QString urlParameters = QString());
+    void parseInterestingPeopleData(const QByteArray &jsonReply);
 
     /**
-    * @brief Forms url parameters
+    * @brief Parses messages data from JSON string
     *
-    * @param coordinates current coordinates
-    * @param status optional status message
-    * @param publish optional publish location on Facebook wall (true/false)
-    * @return QString
+    * @param jsonReply JSON string
     */
-    QString formUrlParameters(const QPointF &coordinates, QString status = QString(),
-                              QString publish = QString());
+    void parseMessagesData(const QByteArray &jsonReply);
 
     /**
-    * @brief Parses user and friend data from JSON string
+    * @brief Parses popular tags data from JSON string
     *
     * @param jsonReply JSON string
     */
-    void parseUserData(const QByteArray &jsonReply);
+    void parsePopularTagsData(const QByteArray &jsonReply);
 
     /**
-    * @brief Sends http request
+    * @brief Parses reply from JSON string
     *
-    * @param url destination
-    * @param cookieType type of the cookie
-    * @param cookie http cookie
+    * Calls different parse methods or emits error signal if response contains error status.
     */
-    void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
+    void parseReply(const QByteArray &jsonReply, RequestName requestName);
 
-private slots:
+    /**
+    * @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
     *
@@ -197,11 +314,40 @@ signals:
     /**
     * @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 Signal when fetchPeopleWithSimilarInterest request is finished
+    *
+    * @param friends list of friends
+    * @param others list of other people
+    */
+    void interestingPeopleReceived(QList<User> &friends, QList<User> &others);
 
-    void imageReady(User *user);
+    /**
+    * @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
@@ -212,9 +358,8 @@ signals:
 
     /**
     * @brief Signals when updateLocation request finished successfully
-    *
     */
-    void updateWasSuccessful();
+    void updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod);
 
     /**
     * @brief Signals when user data is retrieved
@@ -227,18 +372,17 @@ signals:
 /*******************************************************************************
  * 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<User *> m_friendsList;                ///< List of friends(User)
+    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
 };