Added user class and related unit-tests, implemented user data fetching, re-factored...
[situare] / src / situareservice / situareservice.h
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22
23 #ifndef SITUARESERVICE_H
24 #define SITUARESERVICE_H
25
26 #include <QObject>
27 #include "../facebookservice/facebookauthentication.h"
28 #include "../facebookservice/facebookcredentials.h"
29 #include "../user/user.h"
30
31 class QNetworkAccessManager;
32 class QNetworkReply;
33 class QNetworkRequest;
34 class QPointF;
35 class QUrl;
36
37 /**
38 * @brief SituareService class for communicating with Situare server
39 *
40 * @author Henri Lampela
41 * @class SituareService situareservice.h "situareservice/situareservice.h"
42 */
43 class SituareService : public QObject
44 {
45     Q_OBJECT
46
47 public:
48
49     /**
50     * @brief Default constructor
51     *
52     * @param parent instance of parent
53     * @param manager instance of QNetworkAccessManager
54     */
55     SituareService(QObject *parent = 0, QNetworkAccessManager *manager = 0);
56
57     /**
58     * @brief Destructor
59     *
60     */
61     ~SituareService();
62
63     /**
64     * @brief Retrieves location user and friends information from Situare server
65     *
66     */
67     void fetchLocations();
68
69     /**
70     * @brief Translates coordinates to street address via Situare server
71     *
72     * @param coordinates coordinates to be translated
73     */
74     void reverseGeo(const QPointF &coordinates);
75
76     /**
77     * @brief Updates location to the Situare server
78     *
79     * @param coordinates current cordinates
80     * @param status message
81     * @param publish publish location on Facebook wall (true/false)
82     */
83     void updateLocation(const QPointF &coordinates, const QString &status, const bool &publish);
84
85 public slots:
86
87     /**
88     * @brief Public slot, which indicates when http request has been completed
89     *
90     * @param reply storage for http reply
91     */
92     void requestFinished(QNetworkReply *reply);
93
94
95     /**
96     * @brief Public slot, which indicates when facebook credentials are ready
97     *
98     */
99     void credentialsReady();
100
101 private:
102
103     /**
104     * @brief Forms a http url
105     *
106     * @param baseUrl Server url
107     * @param phpScript Server script
108     * @param urlParameters optional parameters for url
109     * @return QUrl formed url
110     */
111     QUrl formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters = 0);
112
113     /**
114     * @brief Forms url parameters
115     *
116     * @param coordinates current coordinates
117     * @param status optional status message
118     * @param publish optional publish location on Facebook wall (true/false)
119     * @return QString
120     */
121     QString formUrlParameters(const QPointF &coordinates, QString status = 0, QString publish = 0);
122
123     /**
124     * @brief Sends http request
125     *
126     * @param url destination
127     * @param cookieType type of the cookie
128     * @param cookie http cookie
129     */
130     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
131
132     /**
133     * @brief Parses user and friend data from JSON string
134     *
135     * @param jsonReply JSON string
136     */
137     void parseUserData(const QByteArray &jsonReply);
138
139 signals:
140
141     /**
142     * @brief Signals error
143     *
144     * @param error error message
145     */
146     void error(const QString &error);
147
148
149     /**
150     * @brief Signals when user data is retrieved
151     *
152     * @param user instance of user
153     * @param friendList list of friends
154     */
155     void userDataChanged(User &user, QList<User *> &friendList); // not final
156
157 private:
158
159     FacebookCredentials m_credentials; ///< handle for FacebookCredentials
160     QList<QNetworkReply *> m_currentRequests; ///< List of current http requests
161     FacebookAuthentication m_facebookAuthentication; ///< Pointer to FacebookAuthentication
162     QNetworkAccessManager *m_networkManager; ///< Pointer to QNetworkAccessManager
163     //QList<User *> m_friendList;
164     //User m_user;
165 };
166
167 #endif // SITUARESERVICE_H