Documenting and some clean-up
[situare] / src / situareservice / situareservice.h
index 72d5b3e..a85127c 100644 (file)
 #define SITUARESERVICE_H
 
 #include <QObject>
-#include <QPointF>
-#include <QNetworkAccessManager>
-#include <QNetworkRequest>
-#include <QNetworkReply>
-#include <QUrl>
+
+#include "../user/user.h"
+#include "imagefetcher.h"
+
+class NetworkAccessManager;
+class QNetworkReply;
+class QNetworkRequest;
+class GeoCoordinate;
+class QUrl;
 
 /**
 * @brief SituareService class for communicating with Situare server
 *
+* @author Henri Lampela
 * @class SituareService situareservice.h "situareservice/situareservice.h"
 */
 class SituareService : public QObject
@@ -44,86 +49,237 @@ public:
     /**
     * @brief Default constructor
     *
-    * @fn SituareService
     * @param parent instance of parent
-    * @param manager instance of QNetworkAccessManager
     */
-    SituareService(QObject *parent = 0, QNetworkAccessManager *manager = 0);
+    SituareService(QObject *parent = 0);
+
+    /**
+    * @brief Destructor
+    *
+    */
     ~SituareService();
 
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+
+    /**
+    * @brief Retrieves location user and friends information from Situare server
+    *
+    */
+    void fetchLocations();
+
+    /**
+    * @brief Translates coordinates to street address via Situare server
+    *
+    * @param coordinates coordinates to be translated
+    */
+    void reverseGeo(const GeoCoordinate &coordinates);
+
     /**
     * @brief Updates location to the Situare server
     *
-    * @fn updateLocation
     * @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(QPointF coordinates, QString status, bool publish);
+    void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
+
+public slots:
 
     /**
-    * @brief Translates coordinates to street address via Situare server
+    * @brief Public slot, to clear user data
     *
-    * @fn reverseGeo
-    * @param coordinates coordinates to be translated
     */
-    void reverseGeo(QPointF coordinates);
+    void clearUserData();
+
+    /**
+    * @brief Public slot, which indicates when http request has been completed
+    *
+    * @param reply storage for http reply
+    */
+    void requestFinished(QNetworkReply *reply);
+
+    /**
+      * @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);
+
+    /**
+      * @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 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 Forms a http cookie
+    *
+    * @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
+    */
+    QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
+                       QString sessionKeyValue, QString sessionSecretValue,
+                       const QString &signatureValue, const QString &localeValue);
 
     /**
     * @brief Forms a http url
     *
-    * @fn formUrl
     * @param baseUrl Server url
     * @param phpScript Server script
     * @param urlParameters optional parameters for url
     * @return QUrl formed url
     */
-    QUrl formUrl(QString baseUrl, QString phpScript, QString urlParameters = 0);
+    QUrl formUrl(const QString &baseUrl, const QString &phpScript,
+                 QString urlParameters = QString());
 
     /**
     * @brief Forms url parameters
     *
-    * @fn formUrlParameters
     * @param coordinates current coordinates
     * @param status optional status message
-    * @param publish optional publish location on Facebook wall (true/false)
+    * @param publish optional publish location on Facebook wall
     * @return QString
     */
-    QString formUrlParameters(QPointF coordinates, QString status = 0, QString publish = 0);
+    QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
+                              bool publish = false);
+
+    /**
+    * @brief Parses user and friend data from JSON string
+    *
+    * @param jsonReply JSON string
+    */
+    void parseUserData(const QByteArray &jsonReply);
 
     /**
     * @brief Sends http request
     *
-    * @fn sendRequest
     * @param url destination
     * @param cookieType type of the cookie
     * @param cookie http cookie
     */
-    void sendRequest(QUrl url, QString cookieType, QString cookie);
+    void sendRequest(const QUrl &url, const QString &cookieType, const QString &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> &parameters);
+
+    /**
+      * @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
+    */
+    void imageReceived(const QUrl &url, const QPixmap &image);
+
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
 
 signals:
 
     /**
     * @brief Signals error
     *
-    * @fn error
-    * @param error error message
+    * @param context error context
+    * @param error error code
     */
-    void error(const QString &error);
+    void error(const int context, const int error);
 
-public slots:
+    /**
+    * @brief Signal for image fetching
+    *
+    * @param url Image url
+    */
+    void fetchImage(const QUrl &url);
 
     /**
-    * @brief Public slot, which indicates when http request has been completed
+    * @brief Signals when user's/friend's image is downloaded
     *
-    * @fn requestFinished
-    * @param reply storage for http reply
+    * @param user Instance of user/friend
     */
-    void requestFinished(QNetworkReply *reply);
+    void imageReady(User *user);
+
+    /**
+    * @brief Signals when address data is retrieved
+    *
+    * @param address Street address
+    */
+    void reverseGeoReady(const QString &address);
+
+    /**
+    * @brief Signals when updateLocation request finished successfully
+    *
+    */
+    void updateWasSuccessful();
+
+    /**
+    * @brief Signals when user data is retrieved
+    *
+    * @param user instance of user
+    * @param friendList list of friends
+    */
+    void userDataChanged(User *user, QList<User *> &friendList);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
 
 private:
-    QNetworkAccessManager *networkManager;
-    QList<QNetworkReply *> currentRequests;
+
+    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;    ///< Requests waiting for access_token
+    QList<User *> m_friendsList;                    ///< List of friends(User)
+
+    QString m_session;                          ///< Session data
+
+    NetworkAccessManager *m_networkManager;     ///< Pointer to QNetworkAccessManager
+
+    ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
+    User *m_user;                               ///< Pointer to User
 };
 
 #endif // SITUARESERVICE_H