|
1 /** |
|
2 * Copyright (c) 2010 Sasken Communication Technologies Ltd. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html" |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Nalina Hariharan |
|
14 * |
|
15 * Description: |
|
16 * The Plugin that manages post operations on the logged in user's facebook account |
|
17 * |
|
18 */ |
1 |
19 |
2 // Include files |
20 // Include files |
3 #include <QtPlugin> |
21 #include <QtPlugin> |
4 #include <QCryptographicHash> |
22 #include <QCryptographicHash> |
5 #include <QTextFormat> |
23 #include <QTextFormat> |
6 #include <QTextStream> |
|
7 #include <QFile> |
24 #include <QFile> |
8 #include <QNetworkReply> |
25 #include <QVariant> |
|
26 #include <QListIterator> |
|
27 #include <QDebug> |
|
28 #ifdef SMF_XMLPARSING |
9 #include <QXmlStreamReader> |
29 #include <QXmlStreamReader> |
|
30 #endif |
10 |
31 |
11 #include "fbpostproviderplugin.h" |
32 #include "fbpostproviderplugin.h" |
12 |
33 |
13 // Added for flickr testing - start - put your registered app's keys here |
34 static int chance = 0; |
14 static const QString apiKey = ""; |
35 QString uid; |
15 static const QString apiSecret = ""; |
36 #ifdef SMF_XMLPARSING |
16 static const QString sessionKey = ""; |
37 SmfPost mypost; |
17 static const QString sessionSecret = ""; |
38 SmfContact contact; |
18 |
39 QString currentId; |
|
40 #endif |
|
41 QMap<QString, QString> idNameMap; |
19 |
42 |
20 /** |
43 /** |
21 * Method called by plugins to generate a signature string from a base string |
44 * Method called by plugins to generate a signature string from a base string |
22 * @param aBaseString The base string |
45 * @param aBaseString The base string |
23 * @return The md5 hash of the base string |
46 * @return The md5 hash of the base string |
24 */ |
47 */ |
25 QString FBPostProviderPlugin::generateSignature(const QString aBaseString) |
48 QString FBPostProviderPlugin::generateSignature(const QString aBaseString) |
26 { |
49 { |
27 writeLog("FBPostProviderPlugin::generateSignature"); |
50 qDebug()<<"Inside FBPostProviderPlugin::generateSignature()"; |
28 |
51 |
29 // Create md5 hash of the signature string |
52 // Create md5 hash of the signature string |
30 QByteArray byteArray; |
53 QByteArray byteArray; |
31 byteArray.insert(0, aBaseString.toAscii()); |
54 byteArray.insert(0, aBaseString.toAscii()); |
32 |
55 |
33 QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex(); |
56 QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex(); |
34 QString returnString (md5Hash); |
57 QString returnString (md5Hash); |
35 return returnString; |
58 return returnString; |
36 } |
59 } |
37 |
60 |
38 |
|
39 /** |
|
40 * Method called by plugins for logging |
|
41 * @param log string to be logged |
|
42 */ |
|
43 void FBPostProviderPlugin::writeLog(QString log) const |
|
44 { |
|
45 QFile file("c:\\data\\PluginLogs.txt"); |
|
46 if (!file.open(QIODevice::Append | QIODevice::Text)) |
|
47 ; |
|
48 QTextStream out(&file); |
|
49 out << log << "\n"; |
|
50 file.close(); |
|
51 } |
|
52 |
|
53 /** |
61 /** |
54 * Destructor |
62 * Destructor |
55 */ |
63 */ |
56 FBPostProviderPlugin::~FBPostProviderPlugin( ) |
64 FBPostProviderPlugin::~FBPostProviderPlugin( ) |
57 { |
65 { |
58 if(m_provider) |
66 if(m_provider) |
59 delete m_provider; |
67 delete m_provider; |
60 } |
68 } |
61 |
69 |
62 /** |
70 /** |
63 * Method that returns maximum no of chars (unicode) that service |
71 * Method that returns maximum no of chars (unicode) that service |
64 * provider can post without truncation. Negative value means |
72 * provider can post without truncation. Negative value means |
65 * no limit |
73 * no limit |
66 * @return Max characters that can be posted without truncation |
74 * @return Max characters that can be posted without truncation |
67 */ |
75 */ |
68 qint32 FBPostProviderPlugin::maxCharsInPost( ) const |
76 qint32 FBPostProviderPlugin::maxCharsInPost( ) const |
69 { |
77 { |
|
78 qDebug()<<"Inside FBPostProviderPlugin::maxCharsInPost()"; |
70 qint32 maxCharsInPost = 256; |
79 qint32 maxCharsInPost = 256; |
71 return maxCharsInPost; |
80 return maxCharsInPost; |
72 } |
81 } |
73 /** |
82 /** |
74 * Method that returns maximum no of items that can be returned |
83 * Method that returns maximum no of items that can be returned |
75 * in a single query to getPosts. Negative value means feature |
84 * in a single query to getPosts. Negative value means feature |
76 * not supported. |
85 * not supported. |
77 * @return Max items that can be returned in a single query |
86 * @return Max items that can be returned in a single query |
78 */ |
87 */ |
79 qint32 FBPostProviderPlugin::maxItems( ) const |
88 qint32 FBPostProviderPlugin::maxItems( ) const |
80 { |
89 { |
|
90 qDebug()<<"Inside FBPostProviderPlugin::maxItems()"; |
81 qint32 maxItems = 10; |
91 qint32 maxItems = 10; |
82 return maxItems; |
92 return maxItems; |
83 } |
93 } |
84 |
94 |
85 /** |
95 /** |
86 * <Method that returns all the formatting of posts that this |
96 * <Method that returns all the formatting of posts that this |
87 * service provider supports. May return 0 items to mean |
97 * service provider supports. May return 0 items to mean |
88 * only QString is supported. |
98 * only QString is supported. |
89 * @return Supported formats of posts |
99 * @return Supported formats of posts |
90 */ |
100 */ |
91 QVector<QTextFormat> FBPostProviderPlugin::supportedFormats ( ) const |
101 QVector<QTextFormat> FBPostProviderPlugin::supportedFormats ( ) const |
92 { |
102 { |
|
103 qDebug()<<"Inside FBPostProviderPlugin::supportedFormats()"; |
93 QVector<QTextFormat> data; |
104 QVector<QTextFormat> data; |
94 return data; |
105 return data; |
95 } |
106 } |
96 |
107 |
97 /** |
108 /** |
98 * Method that returns whether this SP supports Appearence |
109 * Method that returns whether this SP supports Appearence |
99 * @return Returns true if Appearance is supported, else false. |
110 * @return Returns true if Appearance is supported, else false. |
100 * @see SmfAppearenceInfo |
111 * @see SmfAppearenceInfo |
101 */ |
112 */ |
102 bool FBPostProviderPlugin::supportsAppearence ( ) const |
113 bool FBPostProviderPlugin::supportsAppearence ( ) const |
103 { |
114 { |
|
115 qDebug()<<"Inside FBPostProviderPlugin::supportsAppearence()"; |
104 return false; |
116 return false; |
105 } |
117 } |
106 |
118 |
107 /** |
119 /** |
108 * Method to get the latest posts |
120 * Method to get the latest posts |
109 * @param aRequest [out] The request data to be sent to network |
121 * @param aRequest [out] The request data to be sent to network |
110 * @param aUser The user's contact in this SP, omit for self contact |
122 * @param aUser The user's contact in this SP, omit for self contact |
112 * @param aItemsPerPage Number of items per page |
124 * @param aItemsPerPage Number of items per page |
113 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
125 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
114 */ |
126 */ |
115 SmfPluginError FBPostProviderPlugin::retrieve( SmfPluginRequestData &aRequest, |
127 SmfPluginError FBPostProviderPlugin::retrieve( SmfPluginRequestData &aRequest, |
116 const SmfContact *aUser, |
128 const SmfContact *aUser, |
117 const int aPageNum , |
129 const int aPageNum , |
118 const int aItemsPerPage ) |
130 const int aItemsPerPage ) |
119 { |
131 { |
120 Q_UNUSED(aUser) |
132 Q_UNUSED(aUser) |
121 writeLog("Inside FBPostProviderPlugin::retrieve"); |
133 qDebug()<<"Inside FBPostProviderPlugin::retrieve()"; |
122 |
134 |
123 SmfPluginError error = SmfPluginErrInvalidRequest; |
135 //// Currently considering for self contact , ie, omitting aUser |
124 |
136 if(0 == chance) |
125 //// Currently considering for self contatc , ie, omitting aUser |
137 return getFacebookUserId(aRequest); |
|
138 else |
|
139 return getPosts(aRequest, aPageNum , aItemsPerPage); |
|
140 } |
|
141 |
|
142 |
|
143 /** |
|
144 * Method to get the user's facebook ID |
|
145 * @param aRequest [out] The request data to be sent to network |
|
146 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
147 */ |
|
148 SmfPluginError FBPostProviderPlugin::getFacebookUserId( |
|
149 SmfPluginRequestData &aRequest ) |
|
150 { |
|
151 qDebug()<<"Inside FBPostProviderPlugin::getFacebookUserId()"; |
|
152 |
|
153 SmfPluginError error = SmfPluginErrUserNotLoggedIn; |
|
154 |
|
155 #if 1 |
|
156 // Reading the keys, CSM Stubbed - START |
|
157 QFile file("c:\\data\\FacebookKeys.txt"); |
|
158 if (!file.open(QIODevice::ReadOnly)) |
|
159 { |
|
160 qDebug()<<"File to read the keys could not be opened"; |
|
161 return error; |
|
162 } |
|
163 |
|
164 qDebug()<<"Key file read, going to parse the key values from file"; |
|
165 |
|
166 QByteArray arr = file.readAll(); |
|
167 QList<QByteArray> list = arr.split('\n'); |
|
168 file.close(); |
|
169 |
|
170 QString apiKey(list[0]); |
|
171 QString apiSecret(list[1]); |
|
172 QString sessionKey(list[2]); |
|
173 QString sessionSecret(list[3]); |
|
174 |
|
175 qDebug()<<"Api Key = "+apiKey; |
|
176 qDebug()<<"Api Secret = "+apiSecret; |
|
177 qDebug()<<"session Key = "+sessionKey; |
|
178 qDebug()<<"session Secret = "+sessionSecret; |
|
179 // Reading the keys, CSM Stubbed - END |
|
180 #endif |
|
181 |
|
182 // Get the current date and time and convert it to sec as a string |
|
183 QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10); |
|
184 |
|
185 // Create the API signature string |
|
186 QString baseString; |
|
187 baseString.append("api_key="+apiKey); |
|
188 baseString.append("call_id="+call_id); |
|
189 #ifdef SMF_XMLPARSING |
|
190 baseString.append("format=XML"); |
|
191 #else |
|
192 baseString.append("format=JSON"); |
|
193 #endif |
|
194 baseString.append("method=users.getLoggedInUser"); |
|
195 baseString.append("session_key="+sessionKey); |
|
196 baseString.append("ss=1"); |
|
197 baseString.append("v=1.0"); |
|
198 baseString.append(sessionSecret); |
|
199 |
|
200 // Create the url |
|
201 QUrl url("http://api.facebook.com/restserver.php?"); |
|
202 url.addQueryItem("api_key", apiKey); |
|
203 url.addQueryItem("call_id", call_id); |
|
204 #ifdef SMF_XMLPARSING |
|
205 url.addQueryItem("format", "XML"); |
|
206 #else |
|
207 url.addQueryItem("format", "JSON"); |
|
208 #endif |
|
209 url.addQueryItem("method", "users.getLoggedInUser"); |
|
210 url.addQueryItem("session_key", sessionKey); |
|
211 url.addQueryItem("ss", "1"); |
|
212 url.addQueryItem("v", "1.0"); |
|
213 url.addQueryItem("sig", generateSignature(baseString)); |
|
214 |
|
215 // Create the request, set the url |
|
216 aRequest.iNetworkRequest.setUrl(url); |
|
217 aRequest.iRequestType = SmfContactRetrievePosts; |
|
218 aRequest.iPostData = NULL; |
|
219 aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; |
|
220 error = SmfPluginErrNone; |
|
221 |
|
222 qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString(); |
|
223 return error; |
|
224 } |
|
225 |
|
226 /** |
|
227 * Method to get the user's posts |
|
228 * @param aRequest [out] The request data to be sent to network |
|
229 * @param aPageNum The page to be extracted |
|
230 * @param aItemsPerPage Number of items per page |
|
231 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
232 */ |
|
233 SmfPluginError FBPostProviderPlugin::getPosts( |
|
234 SmfPluginRequestData &aRequest, |
|
235 const int aPageNum , |
|
236 const int aItemsPerPage ) |
|
237 { |
|
238 qDebug()<<"Inside FBPostProviderPlugin::getPosts()"; |
|
239 qDebug()<<"aPageNum = "<<aPageNum; |
|
240 qDebug()<<"aItemsPerPage = "<<aItemsPerPage; |
|
241 |
|
242 SmfPluginError error = SmfPluginErrInvalidArguments; |
|
243 |
126 // invalid arguments |
244 // invalid arguments |
127 if( aPageNum < 0 || aItemsPerPage < 0 ) |
245 if( aPageNum < 0 || aItemsPerPage < 0 ) |
|
246 { |
|
247 qDebug()<<"Invalid arguments"; |
128 return error; |
248 return error; |
|
249 } |
|
250 |
|
251 qDebug()<<"Valid arguments"; |
|
252 |
|
253 #if 1 |
|
254 // Reading the keys, CSM Stubbed - START |
|
255 QFile file("c:\\data\\FacebookKeys.txt"); |
|
256 if (!file.open(QIODevice::ReadOnly)) |
|
257 { |
|
258 qDebug()<<"File to read the keys could not be opened"; |
|
259 return SmfPluginErrUserNotLoggedIn; |
|
260 } |
|
261 |
|
262 qDebug()<<"Key file read, going to parse the key values from file"; |
|
263 |
|
264 QByteArray arr = file.readAll(); |
|
265 QList<QByteArray> list = arr.split('\n'); |
|
266 file.close(); |
|
267 |
|
268 QString apiKey(list[0]); |
|
269 QString apiSecret(list[1]); |
|
270 QString sessionKey(list[2]); |
|
271 QString sessionSecret(list[3]); |
|
272 |
|
273 qDebug()<<"Api Key = "+apiKey; |
|
274 qDebug()<<"Api Secret = "+apiSecret; |
|
275 qDebug()<<"session Key = "+sessionKey; |
|
276 qDebug()<<"session Secret = "+sessionSecret; |
|
277 // Reading the keys, CSM Stubbed - END |
|
278 #endif |
|
279 |
|
280 // Get the current date and time and convert it to sec as a string |
|
281 QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10); |
|
282 |
|
283 // Create the API signature string |
|
284 QString baseString; |
|
285 baseString.append("api_key="+apiKey); |
|
286 baseString.append("call_id="+call_id); |
|
287 #ifdef SMF_XMLPARSING |
|
288 baseString.append("format=XML"); |
|
289 #else |
|
290 baseString.append("format=JSON"); |
|
291 #endif |
|
292 if(0 == aPageNum) |
|
293 { |
|
294 //baseString.append("limit="+QString::number((aItemsPerPage*(aPageNum+1)), 10)); |
|
295 qDebug()<<"Hardcoding pagenumber and itemperpage to fetch 5 posts"; |
|
296 int val = 5; |
|
297 baseString.append("limit="+QString::number(val)); |
|
298 } |
129 else |
299 else |
|
300 baseString.append("limit="+QString::number((aItemsPerPage*aPageNum), 10)); |
|
301 baseString.append("method=stream.get"); |
|
302 baseString.append("session_key="+sessionKey); |
|
303 baseString.append("source_ids="+uid); |
|
304 baseString.append("ss=1"); |
|
305 baseString.append("v=1.0"); |
|
306 baseString.append(sessionSecret); |
|
307 |
|
308 // Create the url |
|
309 QUrl url("http://api.facebook.com/restserver.php?"); |
|
310 url.addQueryItem("api_key", apiKey); |
|
311 url.addQueryItem("call_id", call_id); |
|
312 #ifdef SMF_XMLPARSING |
|
313 url.addQueryItem("format", "XML"); |
|
314 #else |
|
315 url.addQueryItem("format", "JSON"); |
|
316 #endif |
|
317 if(0 == aPageNum) |
130 { |
318 { |
131 // Get the current date and time and convert it to sec as a string |
319 // url.addQueryItem("limit", QString::number((aItemsPerPage*(aPageNum+1)), 10)); |
132 QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10); |
320 qDebug()<<"Hardcoding pagenumber and itemperpage to fetch 5 posts"; |
133 |
321 int val = 5; |
134 // Create the API signature string |
322 url.addQueryItem("limit", QString::number(val)); |
135 QString baseString; |
|
136 baseString.append("api_key="+apiKey); |
|
137 baseString.append("call_id="+call_id); |
|
138 baseString.append("format=XML"); |
|
139 baseString.append("method=stream.get"); |
|
140 baseString.append("session_key="+sessionKey); |
|
141 baseString.append("ss=1"); |
|
142 baseString.append("v=1.0"); |
|
143 baseString.append(sessionSecret); |
|
144 |
|
145 // Create the url |
|
146 QUrl url("http://api.facebook.com/restserver.php?"); |
|
147 url.addQueryItem("api_key", apiKey); |
|
148 url.addQueryItem("call_id", call_id); |
|
149 url.addQueryItem("format", "XML"); |
|
150 url.addQueryItem("method", "stream.get"); |
|
151 url.addQueryItem("session_key", sessionKey); |
|
152 url.addQueryItem("ss", "1"); |
|
153 url.addQueryItem("v", "1.0"); |
|
154 url.addQueryItem("sig", generateSignature(baseString)); |
|
155 |
|
156 // Create the request, set the url |
|
157 aRequest.iNetworkRequest.setUrl(url); |
|
158 aRequest.iRequestType = SmfContactRetrievePosts; |
|
159 aRequest.iPostData = NULL; |
|
160 aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; |
|
161 error = SmfPluginErrNone; |
|
162 } |
323 } |
163 writeLog("Url string is : "+aRequest.iNetworkRequest.url().toString()); |
324 else |
|
325 url.addQueryItem("limit", QString::number((aItemsPerPage*aPageNum), 10)); |
|
326 url.addQueryItem("method", "stream.get"); |
|
327 url.addQueryItem("session_key", sessionKey); |
|
328 url.addQueryItem("source_ids",uid); |
|
329 url.addQueryItem("ss", "1"); |
|
330 url.addQueryItem("v", "1.0"); |
|
331 url.addQueryItem("sig", generateSignature(baseString)); |
|
332 |
|
333 // Create the request, set the url |
|
334 aRequest.iNetworkRequest.setUrl(url); |
|
335 aRequest.iRequestType = SmfContactRetrievePosts; |
|
336 aRequest.iPostData = NULL; |
|
337 aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; |
|
338 error = SmfPluginErrNone; |
|
339 |
|
340 qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString(); |
164 return error; |
341 return error; |
165 } |
342 } |
166 |
343 |
167 /** |
344 /** |
168 * Method to update a post to own area. |
345 * Method to update a post to own area. |
204 * @param aContact The contact where the data has to be posted |
383 * @param aContact The contact where the data has to be posted |
205 * @param aLocation The location |
384 * @param aLocation The location |
206 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
385 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
207 */ |
386 */ |
208 SmfPluginError FBPostProviderPlugin::postDirected( SmfPluginRequestData &aRequest, |
387 SmfPluginError FBPostProviderPlugin::postDirected( SmfPluginRequestData &aRequest, |
209 const SmfPost &aPostData, |
388 const SmfPost &aPostData, |
210 const SmfContact &aContact, |
389 const SmfContact &aContact, |
211 const SmfLocation *aLocation ) |
390 const SmfLocation *aLocation ) |
212 { |
391 { |
213 Q_UNUSED(aRequest) |
392 Q_UNUSED(aLocation) |
214 Q_UNUSED(aPostData) |
393 qDebug()<<"Inside FBPostProviderPlugin::postDirected()"; |
215 Q_UNUSED(aContact) |
394 |
216 Q_UNUSED(aLocation) |
395 SmfPluginError error = SmfPluginErrInvalidArguments; |
217 SmfPluginError error = SmfPluginErrInvalidRequest; |
396 |
|
397 // invalid arguments |
|
398 qDebug()<<"To be Post data = "<<aPostData.description(); |
|
399 qDebug()<<"To be posted Contact id = "<<aContact.value("Guid").value<QContactGuid>().guid(); |
|
400 |
|
401 if( (0 == aPostData.description().size()) || (0 == aContact.value("Guid").value<QContactGuid>().guid().size()) ) |
|
402 { |
|
403 qDebug()<<"Invalid arguments"; |
|
404 return error; |
|
405 } |
|
406 |
|
407 qDebug()<<"Valid arguments"; |
|
408 |
|
409 #if 1 |
|
410 // Reading the keys, CSM Stubbed - START |
|
411 QFile file("c:\\data\\FacebookKeys.txt"); |
|
412 if (!file.open(QIODevice::ReadOnly)) |
|
413 { |
|
414 qDebug()<<"File to read the keys could not be opened"; |
|
415 return SmfPluginErrUserNotLoggedIn; |
|
416 } |
|
417 |
|
418 qDebug()<<"Key file read, going to parse the key values from file"; |
|
419 |
|
420 QByteArray arr = file.readAll(); |
|
421 QList<QByteArray> list = arr.split('\n'); |
|
422 file.close(); |
|
423 |
|
424 QString apiKey(list[0]); |
|
425 QString apiSecret(list[1]); |
|
426 QString sessionKey(list[2]); |
|
427 QString sessionSecret(list[3]); |
|
428 |
|
429 qDebug()<<"Api Key = "<<apiKey; |
|
430 qDebug()<<"Api Secret = "<<apiSecret; |
|
431 qDebug()<<"session Key = "<<sessionKey; |
|
432 qDebug()<<"session Secret = "<<sessionSecret; |
|
433 // Reading the keys, CSM Stubbed - END |
|
434 #endif |
|
435 |
|
436 // Get the current date and time and convert it to sec as a string |
|
437 QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10); |
|
438 |
|
439 // Create the API signature string |
|
440 QString baseString; |
|
441 baseString.append("api_key="+apiKey); |
|
442 baseString.append("call_id="+call_id); |
|
443 #ifdef SMF_XMLPARSING |
|
444 baseString.append("format=XML"); |
|
445 #else |
|
446 baseString.append("format=JSON"); |
|
447 #endif |
|
448 baseString.append("message="+aPostData.description()); |
|
449 baseString.append("method=stream.publish"); |
|
450 baseString.append("session_key="+sessionKey); |
|
451 baseString.append("ss=1"); |
|
452 baseString.append("target_id="+aContact.value("Guid").value<QContactGuid>().guid()); |
|
453 baseString.append("v=1.0"); |
|
454 baseString.append(sessionSecret); |
|
455 |
|
456 // Create the url |
|
457 QUrl url("http://api.facebook.com/restserver.php?"); |
|
458 url.addQueryItem("api_key", apiKey); |
|
459 url.addQueryItem("call_id", call_id); |
|
460 #ifdef SMF_XMLPARSING |
|
461 url.addQueryItem("format", "XML"); |
|
462 #else |
|
463 url.addQueryItem("format", "JSON"); |
|
464 #endif |
|
465 url.addQueryItem("message", aPostData.description()); |
|
466 url.addQueryItem("method", "stream.publish"); |
|
467 url.addQueryItem("session_key", sessionKey); |
|
468 url.addQueryItem("ss", "1"); |
|
469 url.addQueryItem("target_id", aContact.value("Guid").value<QContactGuid>().guid()); |
|
470 url.addQueryItem("v", "1.0"); |
|
471 url.addQueryItem("sig", generateSignature(baseString)); |
|
472 |
|
473 // Create the request, set the url |
|
474 aRequest.iNetworkRequest.setUrl(url); |
|
475 aRequest.iRequestType = SmfContactPostDirected; |
|
476 aRequest.iPostData = NULL; |
|
477 aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; |
|
478 error = SmfPluginErrNone; |
|
479 |
|
480 qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString(); |
218 return error; |
481 return error; |
219 } |
482 } |
220 |
483 |
221 |
484 |
222 /** |
485 /** |
324 return m_provider; |
592 return m_provider; |
325 } |
593 } |
326 |
594 |
327 /** |
595 /** |
328 * Method to get the result for a network request. |
596 * Method to get the result for a network request. |
|
597 * @param aOperation The type of operation to be requested |
329 * @param aTransportResult The result of transport operation |
598 * @param aTransportResult The result of transport operation |
330 * @param aResponse The QByteArray instance containing the network response. |
599 * @param aResponse The QByteArray instance containing the network response. |
331 * The plugins should delete this instance once they have read the |
600 * The plugins should delete this instance once they have read the |
332 * data from it. |
601 * data from it. |
333 * @param aResult [out] An output parameter to the plugin manager.If the |
602 * @param aResult [out] An output parameter to the plugin manager.If the |
334 * return value is SmfSendRequestAgain, QVariant will be of type |
603 * return value is SmfSendRequestAgain, QVariant will be of type |
335 * SmfPluginRequestData. |
604 * SmfPluginRequestData. |
336 * For SmfPostProviderPlugin: If last operation was retrieve(), aResult will be |
605 * For SmfPostProviderPlugin: If last operation was retrieve(), aResult will be |
337 * of type QList<SmfPost>. If last operation was post() or updatePost() or |
606 * of type QList<SmfPost>. If last operation was post() or updatePost() or |
338 * postDirected() or commentOnAPost() or postAppearence() or sharePost(), |
607 * postDirected() or commentOnAPost() or postAppearence() or sharePost(), |
339 * aResult will be of type bool |
608 * aResult will be of type bool |
340 * @param aRetType [out] SmfPluginRetType |
609 * @param aRetType [out] SmfPluginRetType |
341 * @param aPageResult [out] The SmfResultPage structure variable |
610 * @param aPageResult [out] The SmfResultPage structure variable |
342 */ |
611 */ |
343 SmfPluginError FBPostProviderPlugin::responseAvailable( |
612 SmfPluginError FBPostProviderPlugin::responseAvailable( |
344 const SmfTransportResult &aTransportResult, |
613 const SmfRequestTypeID aOperation, |
345 QByteArray *aResponse, |
614 const SmfTransportResult &aTransportResult, |
346 QVariant* aResult, |
615 QByteArray *aResponse, |
|
616 QVariant* aResult, |
347 SmfPluginRetType &aRetType, |
617 SmfPluginRetType &aRetType, |
348 SmfResultPage &aPageResult ) |
618 SmfResultPage &aPageResult ) |
349 { |
619 { |
350 writeLog("FBPostProviderPlugin::::responseAvailable"); |
620 qDebug()<<"Inside FBPostProviderPlugin::responseAvailable()"; |
351 Q_UNUSED(aPageResult) |
621 Q_UNUSED(aPageResult) |
352 //This API is slightly changed by Manasij |
622 SmfPluginError error = SmfPluginErrNetworkError; |
353 SmfPluginError error; |
623 |
|
624 if( !aResponse || (0 == aResponse->size()) ) |
|
625 { |
|
626 qDebug()<<"Response is NULL or empty"; |
|
627 aRetType = SmfRequestError; |
|
628 return error; |
|
629 } |
|
630 |
|
631 QByteArray response(*aResponse); |
|
632 delete aResponse; |
|
633 qDebug()<<"FB response = "<<QString(response); |
|
634 qDebug()<<"FB response size = "<<response.size(); |
|
635 |
354 if(SmfTransportOpNoError == aTransportResult) |
636 if(SmfTransportOpNoError == aTransportResult) |
355 { |
637 { |
356 writeLog("No transport error"); |
638 qDebug()<<"No transport error"; |
357 |
639 |
358 // Write the response to a file |
640 if(SmfContactRetrievePosts == aOperation) |
359 QFile file("c:\\data\\fbresponse.txt"); |
|
360 writeLog("response data written to c:\\data\\fbresponse.txt"); |
|
361 if (!file.open(QIODevice::Append | QIODevice::Text)) |
|
362 ; |
|
363 file.write(aResponse->data()); |
|
364 file.close(); |
|
365 |
|
366 QList<SmfPost> list; |
|
367 |
|
368 // For getting contacts |
|
369 QXmlStreamReader xml(aResponse->data()); |
|
370 while (!xml.atEnd()) |
|
371 { |
641 { |
372 xml.readNext(); |
642 qDebug()<<"Response for retrieving posts"; |
373 if (xml.tokenType() == QXmlStreamReader::StartElement) |
643 if(0 == chance) |
374 { |
644 { |
375 // If the tag is contact |
645 chance = 1; |
376 if (xml.name() == "message") |
646 qDebug()<<"For Getting Current logged in User"; |
377 { |
647 |
378 writeLog("message tag found"); |
648 QString errStr; |
379 |
649 errStr.clear(); |
380 SmfPost post; |
650 uid.clear(); |
381 QString descStr(xml.readElementText()); |
651 #ifdef SMF_XMLPARSING |
382 post.setDescription(descStr); |
652 qDebug()<<"Xml parsing"; |
383 |
653 // For getting contacts from xml response |
384 list.append(post); |
654 QXmlStreamReader xml(response); |
|
655 while (!xml.atEnd()) |
|
656 { |
|
657 xml.readNext(); |
|
658 if (xml.tokenType() == QXmlStreamReader::StartElement) |
|
659 { |
|
660 if (xml.name() == "users_getLoggedInUser_response") |
|
661 { |
|
662 qDebug()<<"users_getLoggedInUser_response tag found"; |
|
663 QString message(xml.readElementText()); |
|
664 uid.append(message); |
|
665 qDebug()<<"current logged in uid = "<<uid; |
|
666 } |
|
667 else if("error_msg" == xml.name()) |
|
668 { |
|
669 qDebug()<<"error_msg tag found"; |
|
670 errStr.append(xml.readElementText()); |
|
671 break; |
|
672 } |
|
673 } |
|
674 } |
|
675 #else |
|
676 qDebug()<<"Json parsing"; |
|
677 if(response.contains(QByteArray("error_msg"))) |
|
678 { |
|
679 qDebug()<<"Response contains error, so parse and get the error code"; |
|
680 |
|
681 bool ok; |
|
682 QVariant result = m_util->parse(response, &ok); |
|
683 if (!ok) |
|
684 { |
|
685 qDebug()<<"An error occurred during json parsing, error = "<<m_util->errorString(); |
|
686 aRetType = SmfRequestError; |
|
687 return SmfPluginErrParsingFailed; |
|
688 } |
|
689 else |
|
690 { |
|
691 QVariantMap map = result.toMap(); |
|
692 errStr.append(map["error_msg"].toString()); |
|
693 } |
|
694 } |
|
695 else |
|
696 { |
|
697 qDebug()<<"Response is ok, so don't parse"; |
|
698 uid.append(response); |
|
699 } |
|
700 #endif |
|
701 |
|
702 if(errStr.size()) |
|
703 { |
|
704 qDebug()<<"Response error found = "<<errStr; |
|
705 error = SmfPluginErrInvalidRequest; |
|
706 aRetType = SmfRequestError; |
|
707 aResult->setValue(errStr); |
|
708 } |
|
709 else |
|
710 { |
|
711 qDebug()<<"current logged in uid = "<<uid; |
|
712 aRetType = SmfSendRequestAgain; |
|
713 error = SmfPluginErrNone; |
|
714 } |
|
715 } |
|
716 else |
|
717 { |
|
718 qDebug()<<"For Fetching posts"; |
|
719 |
|
720 QList<SmfPost> list; |
|
721 QString errStr; |
|
722 errStr.clear(); |
|
723 |
|
724 #ifdef SMF_XMLPARSING |
|
725 qDebug()<<"Xml parsing"; |
|
726 |
|
727 // For getting contacts from xml response |
|
728 QXmlStreamReader xml(response); |
|
729 while (!xml.atEnd()) |
|
730 { |
|
731 xml.readNext(); |
|
732 |
|
733 if (xml.tokenType() == QXmlStreamReader::StartElement) |
|
734 { |
|
735 if (xml.name() == "post_id") |
|
736 { |
|
737 QString message(xml.readElementText()); |
|
738 mypost.setId(message); |
|
739 qDebug()<<"post post_id = "<<mypost.id(); |
|
740 } |
|
741 else if (xml.name() == "actor_id") |
|
742 { |
|
743 QString message(xml.readElementText()); |
|
744 |
|
745 QContactGuid guid; |
|
746 guid.setGuid(message); |
|
747 QVariant varGuid = QVariant::fromValue(guid); |
|
748 contact.setValue("Guid", varGuid); |
|
749 mypost.setOwner(contact); |
|
750 qDebug()<<"post actor_id = "<<contact.value("Guid").value<QContactGuid>().guid(); |
|
751 } |
|
752 else if (xml.name() == "message") |
|
753 { |
|
754 QString message(xml.readElementText()); |
|
755 mypost.setDescription(message); |
|
756 qDebug()<<"post message = "<<mypost.description(); |
|
757 } |
|
758 else if (xml.name() == "permalink") |
|
759 { |
|
760 QString message(xml.readElementText()); |
|
761 QUrl url(message); |
|
762 mypost.setUrl(url); |
|
763 qDebug()<<"post url = "<<mypost.url().toString(); |
|
764 } |
|
765 else if(xml.name() == "id") |
|
766 { |
|
767 currentId.clear(); |
|
768 currentId.append(xml.readElementText()); |
|
769 qDebug()<<"post owners id = "<<currentId; |
|
770 } |
|
771 else if(xml.name() == "name") |
|
772 { |
|
773 QString name = xml.readElementText(); |
|
774 idNameMap.insert(currentId, name); |
|
775 currentId.clear(); |
|
776 qDebug()<<"post owners name = "<<name; |
|
777 } |
|
778 else if("error_msg" == xml.name()) |
|
779 { |
|
780 qDebug()<<"error_msg tag found"; |
|
781 errStr.append(xml.readElementText()); |
|
782 break; |
|
783 } |
|
784 } |
|
785 if (xml.tokenType() == QXmlStreamReader::EndElement) |
|
786 { |
|
787 if (xml.name() == "stream_post") |
|
788 { |
|
789 list.append(mypost); |
|
790 } |
|
791 } |
|
792 } |
|
793 // Loop for setting the "Name" of sctor_id, currently only "Guid" is there |
|
794 // in the post.owner member |
|
795 if(0 == errStr.size()) |
|
796 { |
|
797 for (int i=0 ; i <list.count() ; i++) |
|
798 { |
|
799 QContactGuid guid; |
|
800 guid.setGuid(list[i].owner().value("Guid").value<QContactGuid>().guid()); |
|
801 QVariant guidVar = QVariant::fromValue(guid); |
|
802 contact.setValue("Guid", guidVar); |
|
803 |
|
804 QContactName contactName; |
|
805 contactName.setFirstName(idNameMap.value(list[i].owner().value("Guid").value<QContactGuid>().guid())); |
|
806 QVariant nameVar = QVariant::fromValue(contactName); |
|
807 contact.setValue("Name", nameVar); |
|
808 |
|
809 list[i].setOwner(contact); |
|
810 } |
|
811 } |
|
812 #else |
|
813 qDebug()<<"Json parsing"; |
|
814 |
|
815 bool ok; |
|
816 QVariantMap result = m_util->parse(response, &ok).toMap(); |
|
817 if (!ok) |
|
818 { |
|
819 qDebug()<<"An error occurred during json parsing"; |
|
820 aRetType = SmfRequestError; |
|
821 return SmfPluginErrParsingFailed; |
|
822 } |
|
823 |
|
824 chance = 0; |
|
825 |
|
826 if(response.contains(QByteArray("error_msg"))) |
|
827 { |
|
828 errStr.append(result["error_msg"].toString()); |
|
829 } |
|
830 else |
|
831 { |
|
832 QList<QVariant> list1 = result["posts"].toList(); |
|
833 QListIterator<QVariant> iter(list1); |
|
834 while(iter.hasNext()) |
|
835 { |
|
836 SmfPost post; |
|
837 |
|
838 QVariantMap map2 = iter.next().toMap(); |
|
839 qDebug()<<"post_id = "<<map2["post_id"].toString(); |
|
840 qDebug()<<"viewer_id = "<<map2["viewer_id"].toString(); |
|
841 qDebug()<<"message = "<<map2["message"].toString(); |
|
842 qDebug()<<"updated_time = "<<map2["updated_time"].toString(); |
|
843 qDebug()<<"created_time = "<<map2["created_time"].toString(); |
|
844 qDebug()<<"permalink = "<<map2["permalink"].toString(); |
|
845 |
|
846 // Set the posts's description |
|
847 post.setDescription(map2["message"].toString()); |
|
848 |
|
849 // Set the posts's link |
|
850 QUrl url(map2["permalink"].toString()); |
|
851 post.setUrl(url); |
|
852 |
|
853 // Set the posts's Id |
|
854 post.setId(map2["post_id"].toString()); |
|
855 |
|
856 SmfContact contact; |
|
857 QContactGuid guid; |
|
858 guid.setGuid(map2["actor_id"].toString()); |
|
859 QVariant varGuid = QVariant::fromValue(guid); |
|
860 contact.setValue("Guid", varGuid); |
|
861 |
|
862 // Set the posts's owner details |
|
863 post.setOwner(contact); |
|
864 |
|
865 list.append(post); |
|
866 } |
|
867 |
|
868 // Loop for getting the "Name" of actor_id |
|
869 QList<QVariant> list2 = result["profiles"].toList(); |
|
870 QListIterator<QVariant> iter2(list2); |
|
871 while(iter2.hasNext()) |
|
872 { |
|
873 QVariantMap map2 = iter2.next().toMap(); |
|
874 qDebug()<<"owner's id = "<<map2["id"].toString(); |
|
875 qDebug()<<"owner's name = "<<map2["name"].toString(); |
|
876 idNameMap.insert(map2["id"].toString(), map2["name"].toString()); |
|
877 } |
|
878 |
|
879 // Loop for setting the "Name" of actor_id |
|
880 for (int i=0 ; i <list.count() ; i++) |
|
881 { |
|
882 SmfContact contact = list[i].owner(); |
|
883 |
|
884 QContactName contactName; |
|
885 contactName.setFirstName(idNameMap.value(contact.value("Guid").value<QContactGuid>().guid())); |
|
886 qDebug()<<"Name = "<<contactName.firstName(); |
|
887 QVariant nameVar = QVariant::fromValue(contactName); |
|
888 contact.setValue("Name", nameVar); |
|
889 |
|
890 list[i].setOwner(contact); |
|
891 } |
|
892 } |
|
893 #endif |
|
894 if(errStr.size()) |
|
895 { |
|
896 qDebug()<<"Response error found = "<<errStr; |
|
897 error = SmfPluginErrInvalidRequest; |
|
898 aRetType = SmfRequestError; |
|
899 aResult->setValue(errStr); |
|
900 } |
|
901 else |
|
902 { |
|
903 qDebug()<<"list count = "<<list.count(); |
|
904 aResult->setValue(list); |
|
905 aRetType = SmfRequestComplete; |
|
906 error = SmfPluginErrNone; |
385 } |
907 } |
386 } |
908 } |
387 } |
909 } |
388 |
910 else if(SmfContactPostDirected == aOperation) |
389 aResult->setValue(list); |
911 { |
|
912 qDebug()<<"Response for post directed"; |
|
913 |
|
914 bool posted = false; |
|
915 |
|
916 #ifdef SMF_XMLPARSING |
|
917 // For getting contacts from xml response |
|
918 QXmlStreamReader xml(response); |
|
919 while (!xml.atEnd()) |
|
920 { |
|
921 xml.readNext(); |
|
922 if (xml.tokenType() == QXmlStreamReader::StartElement) |
|
923 { |
|
924 if (xml.name() == "stream_publish_response") |
|
925 { |
|
926 qDebug()<<"stream_publish_response tag found"; |
|
927 QString message(xml.readElementText()); |
|
928 qDebug()<<"response content is = "<<message; |
|
929 qDebug()<<"response size is = "<<message.size(); |
|
930 if(message.size()) |
|
931 posted = true; |
|
932 else |
|
933 posted = false; |
|
934 } |
|
935 } |
|
936 } |
|
937 #else |
|
938 bool ok; |
|
939 QVariantMap result = m_util->parse(response, &ok).toMap(); |
|
940 if (!ok) |
|
941 { |
|
942 qDebug()<<"Response cannot be parsed"; |
|
943 posted = true; |
|
944 aRetType = SmfRequestComplete; |
|
945 error = SmfPluginErrNone; |
|
946 } |
|
947 else |
|
948 { |
|
949 qDebug()<<"Error!!!"; |
|
950 aRetType = SmfRequestError; |
|
951 error = SmfPluginErrInvalidRequest; |
|
952 } |
|
953 #endif |
|
954 |
|
955 qDebug()<<"post posted??? = "<<posted; |
|
956 aResult->setValue(posted); |
|
957 } |
|
958 else |
|
959 { |
|
960 qDebug()<<"Service unsupported, currently only SmfContactRetrievePosts and SmfContactPostDirected!!!"; |
|
961 aRetType = SmfRequestError; |
|
962 error = SmfPluginErrServiceNotSupported; |
|
963 } |
|
964 } |
|
965 |
|
966 else if(SmfTransportOpOperationCanceledError == aTransportResult) |
|
967 { |
|
968 qDebug()<<"Operation Cancelled !!!"; |
|
969 error = SmfPluginErrCancelComplete; |
390 aRetType = SmfRequestComplete; |
970 aRetType = SmfRequestComplete; |
391 error = SmfPluginErrNone; |
|
392 } |
971 } |
393 |
972 |
394 else |
973 else |
395 { |
974 { |
396 error = SmfPluginErrInvalidRequest; |
975 qDebug()<<"Transport Error !!!"; |
|
976 error = SmfPluginErrNetworkError; |
397 aRetType = SmfRequestError; |
977 aRetType = SmfRequestError; |
398 } |
978 } |
399 delete aResponse; |
979 |
400 return error; |
980 return error; |
401 } |
981 } |
402 |
982 |
403 |
983 |
404 /** |
984 /** |
461 { |
1041 { |
462 return m_applicationIcon; |
1042 return m_applicationIcon; |
463 } |
1043 } |
464 |
1044 |
465 /** |
1045 /** |
|
1046 * Method to get the list of interfaces that this provider support |
|
1047 * @return List of supported Interafces |
|
1048 */ |
|
1049 QList<QString> FBProviderBase::supportedInterfaces( ) const |
|
1050 { |
|
1051 return m_supportedInterfaces; |
|
1052 } |
|
1053 |
|
1054 /** |
|
1055 * Method to get the list of languages supported by this service provider |
|
1056 * @return a QStringList of languages supported by this service |
|
1057 * provider in 2 letter ISO 639-1 format. |
|
1058 */ |
|
1059 QStringList FBProviderBase::supportedLanguages( ) const |
|
1060 { |
|
1061 return m_supportedLangs; |
|
1062 } |
|
1063 |
|
1064 /** |
466 * Method to get the Plugin specific ID |
1065 * Method to get the Plugin specific ID |
467 * @return The Plugin specific ID |
1066 * @return The Plugin specific ID |
468 */ |
1067 */ |
469 QString FBProviderBase::pluginId( ) const |
1068 QString FBProviderBase::pluginId( ) const |
470 { |
1069 { |
471 return m_pluginId; |
1070 return m_pluginId; |
472 } |
1071 } |
473 |
1072 |
474 /** |
1073 /** |
475 * Method to get the ID of the authentication application |
1074 * Method to get the ID of the authentication application |
476 * for this service |
1075 * for this service |
477 * @param aProgram The authentication application name |
1076 * @param aProgram The authentication application name |
478 * @param aArguments List of arguments required for authentication app |
1077 * @param aArguments List of arguments required for authentication app |
479 * @param aMode Strting mode for authentication application |
1078 * @param aMode Strting mode for authentication application |
480 * @return The ID of the authentication application |
1079 * @return The ID of the authentication application |
481 */ |
1080 */ |
482 QString FBProviderBase::authenticationApp( QString &aProgram, |
1081 QString FBProviderBase::authenticationApp( QString &aProgram, |
483 QStringList & aArguments, |
1082 QStringList & aArguments, |
484 QIODevice::OpenModeFlag aMode ) const |
1083 QIODevice::OpenModeFlag aMode ) const |
485 { |
1084 { |
486 Q_UNUSED(aProgram) |
1085 Q_UNUSED(aProgram) |
487 Q_UNUSED(aArguments) |
1086 Q_UNUSED(aArguments) |
488 Q_UNUSED(aMode) |
1087 Q_UNUSED(aMode) |
489 return m_authAppId; |
1088 return m_authAppId; |
490 } |
1089 } |
491 |
1090 |
492 /** |
1091 /** |
493 * Method to get the unique registration ID provided by the |
1092 * Method to get the unique registration ID provided by the |
494 * Smf for authorised plugins |
1093 * Smf for authorised plugins |
495 * @return The unique registration ID/token provided by the Smf for |
1094 * @return The unique registration ID/token provided by the Smf for |
496 * authorised plugins |
1095 * authorised plugins |
497 */ |
1096 */ |
498 QString FBProviderBase::smfRegistrationId( ) const |
1097 QString FBProviderBase::smfRegistrationId( ) const |
499 { |
1098 { |
500 return m_smfRegToken; |
1099 return m_smfRegToken; |
501 } |
1100 } |
502 |
1101 |
|
1102 /** |
|
1103 * Method that initializes this class. This method should be called |
|
1104 * from the initialize() method of the FBPostProviderPlugin class |
|
1105 */ |
503 void FBProviderBase::initialize() |
1106 void FBProviderBase::initialize() |
504 { |
1107 { |
505 m_serviceName = "Facebook"; |
1108 m_serviceName = "Facebook"; |
506 m_description = "Facebook plugin description"; |
1109 m_description = "Facebook plugin description"; |
507 m_serviceUrl = QUrl(QString("http://api.facebook.com")); |
1110 m_serviceUrl = QUrl(QString("http://api.facebook.com")); |
508 m_pluginId = "fbpostproviderplugin.qtplugin"; |
1111 m_pluginId = "fbpostproviderplugin.qtplugin"; |
509 m_authAppId = "Facebook AuthAppId"; |
1112 m_authAppId = "Facebook AuthAppId"; |
510 m_smfRegToken = "Facebook RegToken"; |
1113 m_smfRegToken = "Facebook RegToken"; |
|
1114 m_supportedInterfaces.append("org.symbian.smf.plugin.contact.posts/v0.2"); |
511 } |
1115 } |
512 |
1116 |
513 |
1117 |
514 /* |
1118 /* |
515 * Export Macro |
1119 * Export Macro |