|
1 /* |
|
2 * MusicLyricsPlugin.cpp |
|
3 * |
|
4 * Created on: Aug 18, 2010 |
|
5 * Author: sanpra |
|
6 */ |
|
7 |
|
8 //Include files |
|
9 #include <QtPlugin> |
|
10 #include <QCryptographicHash> |
|
11 #include <QDataStream> |
|
12 #include <QTextStream> |
|
13 #include <QFile> |
|
14 #include <QNetworkReply> |
|
15 #include <QMap> |
|
16 #include <QListIterator> |
|
17 #include <QXmlStreamReader> |
|
18 #include <SmfPicture.h> |
|
19 #include <QDebug> |
|
20 #include "musiclyricsplugin.h" |
|
21 |
|
22 |
|
23 |
|
24 //Added for testing - start |
|
25 //static const QString apiKey = "8823ece35e487bada68fa893d7ecf91e"; |
|
26 static const QString apiKey = "1b76e55254f5f22ae"; |
|
27 //static const QString apiSecret = "20ccb1cc62aa50caf7d8aae31808d429"; |
|
28 //static const QString miniToken = "773193698 "; |
|
29 |
|
30 //QString fullToken = "72157624000326005-e6cba1c7665a778e"; |
|
31 |
|
32 //Payload data array |
|
33 QByteArray payload; |
|
34 QString uids; |
|
35 /** |
|
36 * Method called by plugins for logging |
|
37 * @param log string to be logged |
|
38 */ |
|
39 void MusicLyricsPlugin::writeLog(QString log) const |
|
40 { |
|
41 QFile file("c:\\data\\LyricsPluginLogs.txt"); |
|
42 if (!file.open(QIODevice::Append | QIODevice::Text)) |
|
43 ; |
|
44 QTextStream out(&file); |
|
45 out << log << "\n"; |
|
46 file.close(); |
|
47 } |
|
48 |
|
49 /** |
|
50 * Destructor |
|
51 */ |
|
52 MusicLyricsPlugin::~MusicLyricsPlugin() |
|
53 { |
|
54 if(m_provider) |
|
55 delete m_provider; |
|
56 } |
|
57 |
|
58 |
|
59 /** |
|
60 * Method called by plugins to generate a signature string from a base string |
|
61 * @param aBaseString The base string |
|
62 * @return The md5 hash of the base string |
|
63 */ |
|
64 QString MusicLyricsPlugin::generateSignature(const QString aBaseString) |
|
65 { |
|
66 writeLog("MusicLyricsPlugin::generateSignature"); |
|
67 |
|
68 // Create md5 hash of the signature string |
|
69 QByteArray byteArray; |
|
70 byteArray.insert(0, aBaseString.toAscii()); |
|
71 |
|
72 QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex(); |
|
73 QString returnString (md5Hash); |
|
74 return returnString; |
|
75 } |
|
76 |
|
77 |
|
78 SmfPluginError MusicLyricsPlugin::lyrics( SmfPluginRequestData &aRequest, |
|
79 const SmfTrackInfo &aTrack, |
|
80 const int aPageNum , |
|
81 const int aItemsPerPage ) |
|
82 { |
|
83 SmfPluginError error = SmfPluginErrUserNotLoggedIn; |
|
84 |
|
85 writeLog("MusicLyricsPlugin::lyrics()"); |
|
86 |
|
87 QString baseString; |
|
88 //spanda |
|
89 QString Url1 = "http://api.lyricsfly.com/api/txt-api.php?"; |
|
90 Url1 += "i=7a48a610d6c65171a-temporary.API.access";//[USER ID] |
|
91 Url1 += "&"; |
|
92 Url1 += "l=one_love";//[LYRICS TEXT STRING] |
|
93 //till here |
|
94 |
|
95 //baseString.append(apiSecret); |
|
96 /* baseString.append("api_key"+apiKey); |
|
97 //baseString.append("auth_token"); |
|
98 //baseString.append("method=geo.getEvents"); |
|
99 writeLog("Url string is :"+baseString); |
|
100 // Create the url |
|
101 QUrl url("http://api.lyricsfly.com/api/"); |
|
102 //url.addQueryItem("method", "geo.getevents"); |
|
103 //url.addQueryItem("location","madrid"); |
|
104 url.addQueryItem("api_key", apiKey); |
|
105 url.addQueryItem("api_sig", generateSignature(baseString)); |
|
106 writeLog("/n/n"+generateSignature(baseString)); |
|
107 writeLog("/n/n Response is :"+url.toString());*/ |
|
108 // Create the request, set the url |
|
109 aRequest.iNetworkRequest.setUrl(Url1); |
|
110 aRequest.iRequestType = SmfMusicGetLyrics; |
|
111 |
|
112 //aRequest.iPostData = NULL; |
|
113 aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; |
|
114 writeLog("/n/n responseAvailble() is getting called"); |
|
115 error = SmfPluginErrNone; |
|
116 return error; |
|
117 } |
|
118 |
|
119 SmfPluginError MusicLyricsPlugin::subtitles( SmfPluginRequestData &aRequest, |
|
120 const SmfTrackInfo &aTrack, |
|
121 const SmfSubtitleSearchFilter &aFilter , |
|
122 const int aPageNum , |
|
123 const int aItemsPerPage ) |
|
124 { |
|
125 //spanda_13 |
|
126 SmfPluginError error = SmfPluginErrUserNotLoggedIn; |
|
127 |
|
128 writeLog("MusicLyricsPlugin::subtitle()"); |
|
129 |
|
130 QString baseString; |
|
131 //spanda |
|
132 QString Url2 = "http://api.allsubs.org/index.php?"; |
|
133 Url2 += "search=anaconda"; |
|
134 Url2 += "&"; |
|
135 Url2 += "language=en"; |
|
136 Url2 += "&"; |
|
137 Url2 += "limit=3"; |
|
138 |
|
139 |
|
140 aRequest.iNetworkRequest.setUrl(Url2); |
|
141 aRequest.iRequestType = SmfMusicGetSubtitle; |
|
142 |
|
143 //aRequest.iPostData = NULL; |
|
144 aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; |
|
145 //writeLog("/n/n responseAvailble() is getting called"); |
|
146 error = SmfPluginErrNone; |
|
147 return error; |
|
148 //till here |
|
149 } |
|
150 /** |
|
151 * Customised method for SmfMusicEventsPlugin interface |
|
152 * @param aRequest [out] The request data to be sent to network |
|
153 * @param aOperation The operation type (should be known between |
|
154 * the client interface and the plugin) |
|
155 * @param aData The data required to form the request (The type |
|
156 * of data should be known between client and the plugin) |
|
157 * @return SmfPluginError Plugin error if any, else SmfPluginErrNone |
|
158 */ |
|
159 SmfPluginError MusicLyricsPlugin::customRequest( SmfPluginRequestData &aRequest, |
|
160 const int &aOperation, QByteArray *aData ) |
|
161 { |
|
162 Q_UNUSED(aRequest) |
|
163 Q_UNUSED(aOperation) |
|
164 Q_UNUSED(aData) |
|
165 return SmfPluginErrServiceNotSupported; |
|
166 } |
|
167 |
|
168 /** |
|
169 * The first method to be called in the plugin that implements this interface. |
|
170 * If this method is not called, plugin may not behave as expected. |
|
171 * Plugins are expected to save the aUtil handle and use and when required. |
|
172 * @param aUtil The instance of SmfPluginUtil |
|
173 */ |
|
174 void MusicLyricsPlugin::initialize( /*SmfPluginUtil *aUtil*/) |
|
175 { |
|
176 // Save the SmfPluginUtil handle |
|
177 //m_util = aUtil; |
|
178 |
|
179 // Create an instance of MusicProviderBase |
|
180 m_provider = new MusicProviderBase; |
|
181 m_provider->initialize(); |
|
182 } |
|
183 |
|
184 /** |
|
185 * Method to get the provider information |
|
186 * @return Instance of SmfProviderBase |
|
187 */ |
|
188 SmfProviderBase* MusicLyricsPlugin::getProviderInfo( ) |
|
189 { |
|
190 return m_provider; |
|
191 } |
|
192 |
|
193 /** |
|
194 * Method to get the result for a network request. |
|
195 * @param aOperation The type of operation to be requested |
|
196 * @param aTransportResult The result of transport operation |
|
197 * @param aResponse The QByteArray instance containing the network response. |
|
198 * The plugins should delete this instance once they have read the |
|
199 * data from it. |
|
200 * @param aResult [out] An output parameter to the plugin manager.If the |
|
201 * return value is SmfSendRequestAgain, QVariant will be of type |
|
202 * SmfPluginRequestData. |
|
203 * For SmfLyricsServicePlugin: If last operation was lyrics(), aResult will |
|
204 * be of type QList<SmfLyrics>. If last operation was subtitles(), aResult will |
|
205 * be of type QList<SmfSubtitle>. |
|
206 */ |
|
207 SmfPluginError MusicLyricsPlugin::responseAvailable( |
|
208 const SmfRequestTypeID aOperation, |
|
209 const SmfTransportResult &aTransportResult, |
|
210 QByteArray *aResponse, |
|
211 QVariant* aResult, |
|
212 SmfPluginRetType &aRetType, |
|
213 SmfResultPage &aPageResult ) |
|
214 { |
|
215 writeLog("MusicLyricsPlugin::responseAvailable"); |
|
216 Q_UNUSED(aPageResult) |
|
217 SmfPluginError error = SmfPluginErrNetworkError; |
|
218 |
|
219 if( !aResponse || (0 == aResponse->size()) ) |
|
220 { |
|
221 writeLog("Response is NULL or empty"); |
|
222 aRetType = SmfRequestError; |
|
223 return error; |
|
224 } |
|
225 |
|
226 QByteArray response(*aResponse); |
|
227 delete aResponse; |
|
228 |
|
229 //Write the response to a file |
|
230 QFile file("c:\\data\\musiclyricsresponse.txt"); |
|
231 writeLog("response data written to c:\\data\\musiclyricsresponse.txt"); |
|
232 |
|
233 if (!file.open(QIODevice::Append | QIODevice::Text)); |
|
234 file.write(response); |
|
235 file.close(); |
|
236 |
|
237 writeLog("MusicEvent response = "+QString(response)); |
|
238 writeLog("MusicEvent response size = "+QString::number(response.size(), 10)); |
|
239 |
|
240 if(SmfTransportOpNoError == aTransportResult) |
|
241 { |
|
242 qDebug()<<"No transport error"; |
|
243 |
|
244 if(SmfMusicGetLyrics == aOperation ) |
|
245 { |
|
246 QList<SmfLyrics> SmfLyricsList; |
|
247 //spanda_14 |
|
248 qDebug()<<"Xml parsing"; |
|
249 |
|
250 //For getting contacts from xml response |
|
251 QXmlStreamReader xml(response); |
|
252 //int count = 0; |
|
253 while (!xml.atEnd()) |
|
254 { |
|
255 xml.readNext(); |
|
256 if (xml.tokenType() == QXmlStreamReader::StartElement) |
|
257 { |
|
258 SmfLyrics lyrics; |
|
259 //SmfLyrics |
|
260 if (xml.name() == "id") |
|
261 { |
|
262 |
|
263 |
|
264 qDebug()<<"id tag found"; |
|
265 QString id ; |
|
266 QString message(xml.readElementText()); |
|
267 id.append(message); |
|
268 qDebug()<<" IDS : "<< id; |
|
269 lyrics.setId(id); |
|
270 //count++; |
|
271 |
|
272 } |
|
273 if (xml.name() == "tx") |
|
274 { |
|
275 |
|
276 qDebug()<<"tx tag found"; |
|
277 QString tx; |
|
278 QString message(xml.readElementText()); |
|
279 tx.append(message); |
|
280 qDebug()<<" text : "<< tx; |
|
281 QByteArray data; |
|
282 QDataStream stream(&data, QIODevice::ReadWrite); |
|
283 stream<<tx; |
|
284 lyrics.setId(data); |
|
285 //count++; |
|
286 |
|
287 } |
|
288 else if("error_msg" == xml.name()) |
|
289 { |
|
290 qDebug()<<"error_msg tag found"; |
|
291 //errStr.append(xml.readElementText()); |
|
292 break; |
|
293 } |
|
294 } |
|
295 } |
|
296 |
|
297 |
|
298 //aRetType = SmfRequestComplete; |
|
299 error = SmfPluginErrNone; |
|
300 } |
|
301 |
|
302 else if (SmfMusicGetSubtitle == aOperation) |
|
303 { |
|
304 writeLog("SmfMusicGetEventsOnVenue = "); |
|
305 |
|
306 writeLog("Inside SmfMusicGetSubtitle "); |
|
307 QList<SmfSubtitle> SmfSubtitleList; |
|
308 |
|
309 writeLog("Xml parsing"); |
|
310 |
|
311 SmfSubtitle Lyricssubtiltle1; |
|
312 Lyricssubtiltle1.setSubtitleUrl(QUrl("http://www.allsubs.org/subs-download/anaconda/1276543/")); |
|
313 Lyricssubtiltle1.setLanguage("english"); |
|
314 QDateTime dt = QDateTime::currentDateTime(); |
|
315 Lyricssubtiltle1.setReleaseYear(dt); |
|
316 SmfSubtitleList.append(Lyricssubtiltle1); |
|
317 |
|
318 SmfSubtitle Lyricssubtiltle2; |
|
319 Lyricssubtiltle2.setSubtitleUrl(QUrl("http://www.allsubs.org/subs-download/")); |
|
320 Lyricssubtiltle2.setLanguage("hindi"); |
|
321 QDateTime dt1 = QDateTime::currentDateTime(); |
|
322 Lyricssubtiltle2.setReleaseYear(dt1); |
|
323 SmfSubtitleList.append(Lyricssubtiltle2); |
|
324 |
|
325 SmfSubtitle Lyricssubtiltle3; |
|
326 Lyricssubtiltle3.setSubtitleUrl(QUrl("http://www.allsubs.org")); |
|
327 Lyricssubtiltle3.setLanguage("hindi"); |
|
328 QDateTime dt2 = QDateTime::currentDateTime(); |
|
329 Lyricssubtiltle3.setReleaseYear(dt2); |
|
330 SmfSubtitleList.append(Lyricssubtiltle3); |
|
331 |
|
332 //For getting contacts from xml response |
|
333 QXmlStreamReader xml(response); |
|
334 //int count = 0; |
|
335 /*while (!xml.atEnd()) |
|
336 { |
|
337 SmfSubtitle Lyricssubtiltle; |
|
338 xml.readNext(); |
|
339 |
|
340 if(xml.tokenType() == QXmlStreamReader::StartDocument) |
|
341 { |
|
342 continue; |
|
343 } |
|
344 |
|
345 if (xml.tokenType() == QXmlStreamReader::StartElement) |
|
346 { |
|
347 if (xml.name() == "AllSubsAPI") |
|
348 { |
|
349 continue; |
|
350 } |
|
351 //writeLog("START ELEMNT"+xml.name().toString()); |
|
352 if (xml.name() == "items") |
|
353 continue; |
|
354 |
|
355 if (xml.name() == "item") |
|
356 { |
|
357 // xml.readNext(); |
|
358 while(!(xml.tokenType() == QXmlStreamReader::EndElement && |
|
359 xml.name() == "item")) |
|
360 { |
|
361 writeLog("loop"); |
|
362 if(xml.tokenType() == QXmlStreamReader::StartElement) |
|
363 { |
|
364 |
|
365 //} |
|
366 // We've found link. |
|
367 if(xml.name() == "link") |
|
368 { |
|
369 |
|
370 Lyricssubtiltle.setSubtitleUrl(xml.readElementText()); |
|
371 writeLog("link "+ Lyricssubtiltle.subtitleUrl()); |
|
372 writeLog("LINK"+xml.readElementText()); |
|
373 |
|
374 } |
|
375 // We've found email. |
|
376 if(xml.name() == "languages") |
|
377 { |
|
378 Lyricssubtiltle.setLanguage(xml.readElementText()); |
|
379 writeLog("languages "+Lyricssubtiltle.language()); |
|
380 writeLog("languages"+xml.readElementText()); |
|
381 |
|
382 |
|
383 } |
|
384 // We've found website. |
|
385 if(xml.name() == "added_on") |
|
386 { |
|
387 QDateTime dt = QDateTime::fromString(xml.readElementText());//stringToTime() |
|
388 Lyricssubtiltle.setReleaseYear(dt); |
|
389 writeLog("added_on "+Lyricssubtiltle.releaseYear().toString()); |
|
390 writeLog("added_on"+xml.readElementText()); |
|
391 } |
|
392 |
|
393 } |
|
394 SmfSubtitleList.append(Lyricssubtiltle); |
|
395 // ...and next... |
|
396 xml.readNext(); |
|
397 }//while |
|
398 |
|
399 //} |
|
400 |
|
401 }//end OF IF iem |
|
402 // }//end of If |
|
403 }//end of start elementIF |
|
404 |
|
405 |
|
406 |
|
407 }//end of while*/ |
|
408 |
|
409 //} |
|
410 writeLog("End Of Log"); |
|
411 writeLog("Count of subtitlles List:"+QString::number(SmfSubtitleList.count(),10)); |
|
412 aResult->setValue(SmfSubtitleList); |
|
413 aRetType = SmfRequestComplete; |
|
414 |
|
415 aRetType = SmfRequestComplete; |
|
416 error = SmfPluginErrNone; |
|
417 } |
|
418 else |
|
419 { |
|
420 writeLog("Service unsupported!!!"); |
|
421 aRetType = SmfRequestError; |
|
422 error = SmfPluginErrServiceNotSupported; |
|
423 } |
|
424 }//end if of if(SmfTransportOpNoError == aTransportResult) |
|
425 |
|
426 else if(SmfTransportOpOperationCanceledError == aTransportResult) |
|
427 { |
|
428 writeLog("Operation Cancelled !!!"); |
|
429 error = SmfPluginErrCancelComplete; |
|
430 aRetType = SmfRequestComplete; |
|
431 } |
|
432 |
|
433 else |
|
434 { |
|
435 writeLog("Transport Error !!!"); |
|
436 error = SmfPluginErrNetworkError; |
|
437 aRetType = SmfRequestError; |
|
438 } |
|
439 |
|
440 return error; |
|
441 } |
|
442 |
|
443 |
|
444 /** |
|
445 * Destructor |
|
446 */ |
|
447 MusicProviderBase::~MusicProviderBase( ) |
|
448 { |
|
449 } |
|
450 |
|
451 /** |
|
452 * Method to get the Localisable name of the service. |
|
453 * @return The Localisable name of the service. |
|
454 */ |
|
455 QString MusicProviderBase::serviceName( ) const |
|
456 { |
|
457 return m_serviceName; |
|
458 } |
|
459 |
|
460 /** |
|
461 * Method to get the Logo of the service |
|
462 * @return The Logo of the service |
|
463 */ |
|
464 QImage MusicProviderBase::serviceIcon( ) const |
|
465 { |
|
466 return m_serviceIcon; |
|
467 } |
|
468 |
|
469 /** |
|
470 * Method to get the Readable service description |
|
471 * @return The Readable service description |
|
472 */ |
|
473 QString MusicProviderBase::description( ) const |
|
474 { |
|
475 return m_description; |
|
476 } |
|
477 |
|
478 /** |
|
479 * Method to get the Website of the service |
|
480 * @return The Website of the service |
|
481 */ |
|
482 QUrl MusicProviderBase::serviceUrl( ) const |
|
483 { |
|
484 return m_serviceUrl; |
|
485 } |
|
486 |
|
487 /** |
|
488 * Method to get the URL of the Application providing this service |
|
489 * @return The URL of the Application providing this service |
|
490 */ |
|
491 QUrl MusicProviderBase::applicationUrl( ) const |
|
492 { |
|
493 return m_applicationUrl; |
|
494 } |
|
495 |
|
496 /** |
|
497 * Method to get the Icon of the application |
|
498 * @return The Icon of the application |
|
499 */ |
|
500 QImage MusicProviderBase::applicationIcon( ) const |
|
501 { |
|
502 return m_applicationIcon; |
|
503 } |
|
504 /** |
|
505 * Method to get the list of interfaces that this provider support |
|
506 * @return List of supported Interafces |
|
507 */ |
|
508 QList<QString> MusicProviderBase::supportedInterfaces( ) const |
|
509 { |
|
510 return m_serviceType; |
|
511 } |
|
512 |
|
513 /** |
|
514 * Method to get the list of languages supported by this service provider |
|
515 * @return a QStringList of languages supported by this service |
|
516 * provider in 2 letter ISO 639-1 format. |
|
517 */ |
|
518 QStringList MusicProviderBase::supportedLanguages( ) const |
|
519 { |
|
520 return m_supportedLanguages; |
|
521 } |
|
522 |
|
523 /** |
|
524 * Method to get the Plugin specific ID |
|
525 * @return The Plugin specific ID |
|
526 */ |
|
527 QString MusicProviderBase::pluginId( ) const |
|
528 { |
|
529 return m_pluginId; |
|
530 } |
|
531 |
|
532 /** |
|
533 * Method to get the ID of the authentication application |
|
534 * for this service |
|
535 * @param aProgram The authentication application name |
|
536 * @param aArguments List of arguments required for authentication app |
|
537 * @param aMode Strting mode for authentication application |
|
538 * @return The ID of the authentication application |
|
539 */ |
|
540 QString MusicProviderBase::authenticationApp( QString &aProgram, |
|
541 QStringList & aArguments, |
|
542 QIODevice::OpenModeFlag aMode ) const |
|
543 { |
|
544 return m_authAppId; |
|
545 } |
|
546 |
|
547 /** |
|
548 * Method to get the unique registration ID provided by the |
|
549 * Smf for authorised plugins |
|
550 * @return The unique registration ID/token provided by the Smf for |
|
551 * authorised plugins |
|
552 */ |
|
553 QString MusicProviderBase::smfRegistrationId( ) const |
|
554 { |
|
555 return m_smfRegToken; |
|
556 } |
|
557 |
|
558 void MusicProviderBase::initialize() |
|
559 { |
|
560 m_serviceName = "LyricsFly"; |
|
561 m_description = "Description"; |
|
562 m_serviceUrl = QUrl(QString("http://api.lyricsfly.com")); |
|
563 m_serviceType << m_serviceName << m_description;// << (QString)(m_serviceUrl); |
|
564 m_supportedLanguages << "English" ; |
|
565 m_pluginId = "musiclyricsplugin.qtplugin"; |
|
566 m_authAppId = "0xEFE2FD23"; |
|
567 m_smfRegToken = "hsdfusdghf"; |
|
568 |
|
569 //sandeep_Added_13-08-2010 |
|
570 /* m_serviceName = "allsubs"; |
|
571 m_description = "Description"; |
|
572 //m_serviceUrl = QUrl(QString//("http://api.allsubs.org")); |
|
573 |
|
574 // m_serviceType << m_serviceName << m_description;// << (QString)(m_serviceUrl); |
|
575 m_supportedLanguages << "English" ; |
|
576 m_pluginId = "musiclyricsplugin.qtplugin"; |
|
577 m_authAppId = "0xEFE2FD23"; |
|
578 m_smfRegToken = "hsdfusdghf"; |
|
579 //m_smfRegToken = "hsdfusdghf"; //yet to be recvd*/ |
|
580 |
|
581 } |
|
582 |
|
583 |
|
584 /* |
|
585 * Export Macro |
|
586 * plugin name : MusicLyricsPlugin |
|
587 * plugin class : MusicLyricsPlugin |
|
588 */ |
|
589 Q_EXPORT_PLUGIN2( musiclyricsplugin, MusicLyricsPlugin ) |