|
1 |
|
2 // Include files |
|
3 #include "sampleplugin.h" |
|
4 #include <QNetworkRequest> |
|
5 #include <QNetworkAccessManager> |
|
6 |
|
7 /** |
|
8 * Constructor with default argument |
|
9 * @param aUtil The SmfPluginUtil instance. The plugins can |
|
10 * call the method getAuthKeys() of this class, with its pluginID to |
|
11 * get the OAuth keys, keys are returned only if this plugin is |
|
12 * authorised by Smf franework |
|
13 */ |
|
14 SamplePlugin::SamplePlugin( SmfPluginUtil *aUtil ) |
|
15 { |
|
16 m_provider = new SampleProviderBase(); |
|
17 m_util = aUtil; |
|
18 } |
|
19 |
|
20 /** |
|
21 * Destructor |
|
22 */ |
|
23 SamplePlugin::~SamplePlugin( ) |
|
24 { |
|
25 if(m_provider) |
|
26 delete m_provider; |
|
27 } |
|
28 |
|
29 /** |
|
30 * Method to get a list of pictures |
|
31 * @param aRequest [out] The request data to be sent to network |
|
32 * @param aPageNum The page to be extracted |
|
33 * @param aItemsPerPage Number of items per page |
|
34 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
35 */ |
|
36 SmfPluginError SamplePlugin::pictures( SmfPluginRequestData &aRequest, |
|
37 const int aPageNum, |
|
38 const int aItemsPerPage ) |
|
39 { |
|
40 SmfPluginError error = SmfPluginErrInvalidRequest; |
|
41 |
|
42 // invalid arguments |
|
43 if( aPageNum < 0 || aItemsPerPage < 0 ) |
|
44 return error; |
|
45 else |
|
46 { |
|
47 // Create a map of the arguments keys and their repective values |
|
48 QMultiMap<QByteArray, QByteArray> params; |
|
49 QString pageNum, itemPerPage; |
|
50 pageNum.number(aPageNum); |
|
51 itemPerPage.number(aItemsPerPage); |
|
52 params.insert("method", "getpictures"); |
|
53 params.insert("pagenumber", pageNum.toAscii()); |
|
54 params.insert("itemsperpage", itemPerPage.toAscii()); |
|
55 |
|
56 QNetworkAccessManager::Operation type = QNetworkAccessManager::GetOperation; |
|
57 SmfSignatureMethod signMethod = HMAC_SHA1; |
|
58 SmfParsingMode mode = ParseForInlineQuery; |
|
59 |
|
60 error = createRequest(aRequest, type, signMethod, params, mode, NULL); |
|
61 } |
|
62 return error; |
|
63 } |
|
64 |
|
65 |
|
66 /** |
|
67 * Method called by plugins to generate a request data |
|
68 * @param aRequest [out] The request data to be sent to network |
|
69 * @param aOperation The type of http operation |
|
70 * @param aSignatureMethod The signature method to be used |
|
71 * @param aParams A map of parameters to its values |
|
72 * @param aMode The mode of creation of the request |
|
73 * @param aPostData The data to be posted (for HTTP POST |
|
74 * only, else it will be NULL) |
|
75 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
76 */ |
|
77 SmfPluginError SamplePlugin::createRequest( SmfPluginRequestData &aRequest, |
|
78 const QNetworkAccessManager::Operation aOperation, |
|
79 const SmfSignatureMethod aSignatureMethod, |
|
80 QMultiMap<QByteArray, QByteArray> &aParams, |
|
81 const SmfParsingMode aMode, |
|
82 QBuffer *aPostData ) |
|
83 { |
|
84 SmfPluginError error; |
|
85 QString url = m_provider->serviceUrl().toString(); |
|
86 |
|
87 // Get the oAuth keys from The Smf Server |
|
88 QMap<QString, QString> keys; |
|
89 m_util->getAuthKeys(keys, m_provider->pluginId()); |
|
90 |
|
91 // Unable to get the tokens |
|
92 if(keys.isEmpty()) |
|
93 error = SmfPluginErrInvalidApplication; |
|
94 else |
|
95 { |
|
96 |
|
97 // Get the token and token secret from keys |
|
98 QByteArray token; |
|
99 QByteArray tokenSecret; |
|
100 token.append(keys.value("oauth_token")); |
|
101 tokenSecret.append(keys.value("oauth_token_secret")); |
|
102 |
|
103 // convert the parameters to string and sign it |
|
104 QByteArray content = m_util->createParameterString(url, aOperation, token, tokenSecret, |
|
105 aSignatureMethod, aParams, aMode ); |
|
106 |
|
107 // Unable to create the signed string |
|
108 if(content.isEmpty()) |
|
109 error = SmfPluginErrInvalidRequest; |
|
110 else |
|
111 { |
|
112 // add the parameter string to the URL |
|
113 url.append(content); |
|
114 |
|
115 // set the url of the request |
|
116 aRequest.iNetworkRequest.setUrl(QUrl(url)); |
|
117 |
|
118 // set the type of http operation to be performed |
|
119 aRequest.iHttpOperationType = aOperation; |
|
120 |
|
121 // As it is a GET operation, set iPostData to NULL |
|
122 aRequest.iPostData = aPostData; |
|
123 |
|
124 // For successful creation of request |
|
125 error = SmfPluginErrNone; |
|
126 } |
|
127 } |
|
128 return error; |
|
129 } |
|
130 |
|
131 |
|
132 /** |
|
133 * Method to get a description |
|
134 * @param aRequest [out] The request data to be sent to network |
|
135 * @param aImage The image abot which the description is required |
|
136 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
137 */ |
|
138 SmfPluginError SamplePlugin::description( SmfPluginRequestData &aRequest, |
|
139 const SmfPicture &aImage ) |
|
140 { |
|
141 SmfPluginError error; |
|
142 |
|
143 // Create a map of the arguments keys and their repective values |
|
144 QMultiMap<QByteArray, QByteArray> params; |
|
145 params.insert("method", "getpictureDescription"); |
|
146 params.insert("photoId", aImage.id().toAscii()); |
|
147 |
|
148 QNetworkAccessManager::Operation type = QNetworkAccessManager::GetOperation; |
|
149 SmfSignatureMethod signMethod = HMAC_SHA1; |
|
150 SmfParsingMode mode = ParseForInlineQuery; |
|
151 |
|
152 error = createRequest(aRequest, type, signMethod, params, mode, NULL); |
|
153 |
|
154 return error; |
|
155 } |
|
156 |
|
157 /** |
|
158 * Method to upload a picture |
|
159 * @param aRequest [out] The request data to be sent to network |
|
160 * @param aImage The image to be uploaded |
|
161 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
162 */ |
|
163 SmfPluginError SamplePlugin::upload( SmfPluginRequestData &aRequest, |
|
164 const SmfPicture &aImage ) |
|
165 { |
|
166 SmfPluginError error = SmfPluginErrInvalidRequest; |
|
167 |
|
168 // Create a map of the arguments keys and their repective values |
|
169 QMultiMap<QByteArray, QByteArray> params; |
|
170 params.insert("method", "upload"); |
|
171 params.insert("title", aImage.title().toAscii()); |
|
172 params.insert("owner", aImage.owner().toAscii()); |
|
173 params.insert("description", aImage.description().toAscii()); |
|
174 params.insert("tags", aImage.tags().join(" ").toAscii()); |
|
175 switch(aImage.visibility()) |
|
176 { |
|
177 case SMFVisibilityFriend: |
|
178 params.insert("isFriend", "true"); |
|
179 break; |
|
180 case SMFVisibilityPublic: |
|
181 params.insert("isPublic", "true"); |
|
182 break; |
|
183 case SMFVisibilityFamily: |
|
184 params.insert("isFamily", "true"); |
|
185 break; |
|
186 case SMFVisibilityGroup: |
|
187 params.insert("isGroup", "true"); |
|
188 break; |
|
189 default:// SMFVisibilityPersonal |
|
190 params.insert("isPrivate", "true"); |
|
191 } |
|
192 |
|
193 QNetworkAccessManager::Operation type = QNetworkAccessManager::PostOperation; |
|
194 SmfSignatureMethod signMethod = HMAC_SHA1; |
|
195 SmfParsingMode mode = ParseForRequestContent; |
|
196 |
|
197 // Write the image as png format to the buffer |
|
198 QByteArray ba; |
|
199 QBuffer buffer(&ba); |
|
200 buffer.open(QIODevice::WriteOnly); |
|
201 aImage.picture().save(&buffer, "PNG"); |
|
202 |
|
203 error = createRequest(aRequest, type, signMethod, params, mode, &buffer); |
|
204 |
|
205 return error; |
|
206 } |
|
207 |
|
208 /** |
|
209 * Method to upload a list of pictures |
|
210 * @param aRequest [out] The request data to be sent to network |
|
211 * @param aImages The list of images to be uploaded |
|
212 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
213 */ |
|
214 SmfPluginError SamplePlugin::upload( SmfPluginRequestData &aRequest, |
|
215 const QList<SmfPicture> &aImages ) |
|
216 { |
|
217 SmfPluginError error; |
|
218 |
|
219 for(int index = 0; index < aImages.count(); index++) |
|
220 { |
|
221 error = upload(aRequest, aImages.value(index)); |
|
222 if(SmfPluginErrNone != error) |
|
223 break; |
|
224 } |
|
225 return error; |
|
226 } |
|
227 |
|
228 /** |
|
229 * Method to post comment on a picture is available |
|
230 * @param aRequest [out] The request data to be sent to network |
|
231 * @param aImage The image on which comment is to be posted |
|
232 * @param aComment The comment to be posted |
|
233 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
234 */ |
|
235 SmfPluginError SamplePlugin::postComment( SmfPluginRequestData &aRequest, |
|
236 const SmfPicture &aImage, |
|
237 const SmfComment &aComment ) |
|
238 { |
|
239 SmfPluginError error = SmfPluginErrInvalidRequest; |
|
240 |
|
241 // Create a map of the arguments keys and their repective values |
|
242 QMultiMap<QByteArray, QByteArray> params; |
|
243 params.insert("method", "postComment"); |
|
244 params.insert("photoId", aImage.id().toAscii()); |
|
245 params.insert("comment", aImage.comments().join(" ").toAscii()); |
|
246 |
|
247 QNetworkAccessManager::Operation type = QNetworkAccessManager::GetOperation; |
|
248 SmfSignatureMethod signMethod = HMAC_SHA1; |
|
249 SmfParsingMode mode = ParseForInlineQuery; |
|
250 |
|
251 error = createRequest(aRequest, type, signMethod, params, mode, NULL); |
|
252 return error; |
|
253 } |
|
254 |
|
255 /** |
|
256 * Method to get the provider information |
|
257 * @return Instance of SmfProviderBase |
|
258 */ |
|
259 SmfProviderBase* SamplePlugin::getProviderInfo( ) |
|
260 { |
|
261 return m_provider; |
|
262 } |
|
263 |
|
264 /** |
|
265 * Method to get the result for a network request. |
|
266 * @param aTransportResult The result of transport operation |
|
267 * @param aReply The QNetworkReply instance for the request |
|
268 * @param aResult [out] An output parameter to the plugin manager.If the |
|
269 * return value is SmfSendRequestAgain, QVariant will be of type |
|
270 * SmfPluginRequestData. |
|
271 * For SmfGalleryPlugin: If last operation was pictures(), aResult will |
|
272 * be of type QList<SmfPicture>. If last operation was description(), |
|
273 * aResult will be of type QString. If last operation was upload() or |
|
274 * postComment(), aResult will be of type bool. |
|
275 * @param aRetType [out] SmfPluginRetType |
|
276 * @param aPageResult [out] The SmfResultPage structure variable |
|
277 */ |
|
278 SmfPluginError SamplePlugin::responseAvailable( |
|
279 const SmfTransportResult &aTransportResult, |
|
280 QNetworkReply *aReply, |
|
281 QVariant* aResult, |
|
282 SmfPluginRetType &aRetType, |
|
283 SmfResultPage &aPageResult ) |
|
284 { |
|
285 SmfPluginError error; |
|
286 if(SmfTransportOpNoError == aTransportResult) |
|
287 { |
|
288 // Assuming a JSON response, parse the response |
|
289 QByteArray response = aReply->readAll(); |
|
290 m_provider->updateDataUsage(0, aReply->readBufferSize()); |
|
291 bool parseResult = false; |
|
292 QVariant *result = new QVariant(); |
|
293 /** see http://qjson.sourceforge.net/usage.html for more details */ |
|
294 parseResult = m_util->getJsonHandle()->parse(response, &parseResult); |
|
295 |
|
296 // For parsing error |
|
297 if(!parseResult) |
|
298 { |
|
299 aRetType = SmfRequestError; |
|
300 error = SmfPluginErrInvalidRequest; |
|
301 } |
|
302 |
|
303 else |
|
304 { |
|
305 // The plugins should convert the result to suitable format, |
|
306 // like if last operation was pictures(), result should be converted to the |
|
307 // type QList<SmfPicture>. If last operation was description(), result should |
|
308 // be converted to the type QString. If last operation was upload() or |
|
309 // postComment(), result should be converted to the type bool. |
|
310 |
|
311 // After conversion, assign the value os result to aResult |
|
312 aResult = result; |
|
313 |
|
314 // if the request is complete |
|
315 aRetType = SmfRequestComplete; |
|
316 |
|
317 // if request need to be sent again |
|
318 aRetType = SmfSendRequestAgain; |
|
319 |
|
320 error = SmfPluginErrNone; |
|
321 } |
|
322 } |
|
323 else |
|
324 { |
|
325 error = SmfPluginErrInvalidRequest; |
|
326 aRetType = SmfRequestError; |
|
327 } |
|
328 |
|
329 return error; |
|
330 } |
|
331 |
|
332 |
|
333 /** |
|
334 * Constructor with default argument |
|
335 * @param aParent The parent object |
|
336 */ |
|
337 SampleProviderBase::SampleProviderBase( QObject* aParent ) |
|
338 : SmfProviderBase(aParent) |
|
339 { |
|
340 } |
|
341 |
|
342 /** |
|
343 * Copy Constructor |
|
344 * @param aOther The reference object |
|
345 */ |
|
346 SampleProviderBase::SampleProviderBase( const SampleProviderBase &aOther ) |
|
347 { |
|
348 } |
|
349 |
|
350 /** |
|
351 * Destructor |
|
352 */ |
|
353 SampleProviderBase::~SampleProviderBase( ) |
|
354 { |
|
355 } |
|
356 |
|
357 /** |
|
358 * Method to get the Localisable name of the service. |
|
359 * @return The Localisable name of the service. |
|
360 */ |
|
361 QString SampleProviderBase::serviceName( ) const |
|
362 { |
|
363 return m_serviceName; |
|
364 } |
|
365 |
|
366 /** |
|
367 * Method to get the Logo of the service |
|
368 * @return The Logo of the service |
|
369 */ |
|
370 QImage SampleProviderBase::serviceIcon( ) const |
|
371 { |
|
372 return m_serviceIcon; |
|
373 } |
|
374 |
|
375 /** |
|
376 * Method to get the Readable service description |
|
377 * @return The Readable service description |
|
378 */ |
|
379 QString SampleProviderBase::description( ) const |
|
380 { |
|
381 return m_description; |
|
382 } |
|
383 |
|
384 /** |
|
385 * Method to get the Website of the service |
|
386 * @return The Website of the service |
|
387 */ |
|
388 QUrl SampleProviderBase::serviceUrl( ) const |
|
389 { |
|
390 return m_serviceUrl; |
|
391 } |
|
392 |
|
393 /** |
|
394 * Method to get the URL of the Application providing this service |
|
395 * @return The URL of the Application providing this service |
|
396 */ |
|
397 QUrl SampleProviderBase::applicationUrl( ) const |
|
398 { |
|
399 return m_applicationUrl; |
|
400 } |
|
401 |
|
402 /** |
|
403 * Method to get the Icon of the application |
|
404 * @return The Icon of the application |
|
405 */ |
|
406 QImage SampleProviderBase::applicationIcon( ) const |
|
407 { |
|
408 return m_applicationIcon; |
|
409 } |
|
410 |
|
411 /** |
|
412 * Method to get the Plugin specific ID |
|
413 * @return The Plugin specific ID |
|
414 */ |
|
415 QString SampleProviderBase::pluginId( ) const |
|
416 { |
|
417 return m_pluginId; |
|
418 } |
|
419 |
|
420 /** |
|
421 * Method to get the ID of the authentication application |
|
422 * for this service |
|
423 * @param aProgram The authentication application name |
|
424 * @param aArguments List of arguments required for authentication app |
|
425 * @param aMode Strting mode for authentication application |
|
426 * @return The ID of the authentication application |
|
427 */ |
|
428 QString SampleProviderBase::authenticationApp( QString &aProgram, |
|
429 QStringList & aArguments, |
|
430 QIODevice::OpenModeFlag aMode ) const |
|
431 { |
|
432 return m_authAppId; |
|
433 } |
|
434 |
|
435 /** |
|
436 * Method to get the unique registration ID provided by the |
|
437 * Smf for authorised plugins |
|
438 * @return The unique registration ID/token provided by the Smf for |
|
439 * authorised plugins |
|
440 */ |
|
441 QString SampleProviderBase::smfRegistrationId( ) const |
|
442 { |
|
443 return m_smfRegToken; |
|
444 } |
|
445 |
|
446 /** |
|
447 * Method to get the data usage of each plugin |
|
448 * @return The data usage structure |
|
449 */ |
|
450 SmfPluginDataUsage SampleProviderBase::getDataUsage( ) const |
|
451 { |
|
452 return m_dataUsage; |
|
453 } |
|
454 |
|
455 /** |
|
456 * Method to update the data usage of this plugin. This method is called |
|
457 * after the plugin sends request to Plugin manager and after it receives |
|
458 * data from plugin manager. |
|
459 * @param aBytesSent The number of bytes sent, when this argument has |
|
460 * some value other than 1, aBytesReceived should be zero. |
|
461 * @param aBytesReceived The number of bytes received, when this argument |
|
462 * has some value other than 1, aBytesSent should be zero. |
|
463 * @return Returns true if success else returns false |
|
464 */ |
|
465 bool SampleProviderBase::updateDataUsage( const uint &aBytesSent, |
|
466 const uint &aBytesReceived ) |
|
467 { |
|
468 bool ret = true; |
|
469 if( aBytesSent && !aBytesReceived ) |
|
470 m_dataUsage.iBytesSent += aBytesSent; |
|
471 else if( !aBytesSent && aBytesReceived ) |
|
472 m_dataUsage.iBytesReceived += aBytesReceived; |
|
473 else |
|
474 // don't update m_dataUsage, error in arguments |
|
475 ret = false; |
|
476 |
|
477 return ret; |
|
478 } |
|
479 |
|
480 |
|
481 /* |
|
482 * Export Macro |
|
483 * plugin name : sampleplugin |
|
484 * plugin class : SamplePlugin |
|
485 */ |
|
486 //Q_EXPORT_PLUGIN2( sampleplugin, SamplePlugin ) |