diff -r b2eb79881f9d -r 574948b60dab example/lastfmmusicsearchplugin/lastfmmusicsearchplugin.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/lastfmmusicsearchplugin/lastfmmusicsearchplugin.cpp Thu Sep 23 17:15:03 2010 +0530 @@ -0,0 +1,1428 @@ +/** + * Copyright (c) 2010 Sasken Communication Technologies Ltd. + * All rights reserved. + * This component and the accompanying materials are made available + * under the terms of the "Eclipse Public License v1.0" + * which accompanies this distribution, and is available + * at the URL "http://www.eclipse.org/legal/epl-v10.html" + * + * Initial Contributors: + * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution + * + * Contributors: + * Nalina Hariharan + * + * Description: + * The Plugin that does music search related functionalities from last.fm site + * + */ + +// Include files +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lastfmmusicsearchplugin.h" + +static int gPageNum = 0; +static int gItemsPerPage = 0; +static quint8 forTracksOfAlbum = 0; +QString albumId; + +int gOperationId; + + +/** + * Destructor + */ +LastFmMusicSearchPlugin::~LastFmMusicSearchPlugin( ) + { + if(m_provider) + delete m_provider; + } + +/** + * Method to interpret the key sets obtained from credential manager + * @param aApiKey [out] The api key + * @param aApiSecret [out] The api secret + * @param aSessionKey [out] The session key + * @param aToken [out] The session token + */ +void LastFmMusicSearchPlugin::fetchKeys( QString &aApiKey, + QString &aApiSecret, + QString &aToken ) + { + qDebug()<<"Reg Token = "<m_smfRegToken; + qDebug()<<"Expiry Date as int = "<m_validity.toTime_t(); + + SmfAuthParams keys; + SmfPluginUtil util; + util.getAuthKeys(keys, m_provider->m_smfRegToken, + m_provider->m_validity, m_provider->m_pluginId); + + QByteArray keyName; + keyName.append("ApiKey"); + aApiKey.append(keys.value(keyName)); + + keyName.clear(); + keyName.append("ApiSecret"); + aApiSecret.append(keys.value(keyName)); + + keyName.clear(); + keyName.append("Token"); + aToken.append(keys.value(keyName)); + + qDebug()<<"Api Key = "<>limit; + read>>pageNum; + read>>album; + + gItemsPerPage = limit; + gPageNum = pageNum; + + // Create the API signature string + QString baseString; + baseString.append("album"+album.name()); + baseString.append("api_key"+apiKey); + baseString.append("limit"+QString::number(limit)); + baseString.append("methodalbum.search"); + baseString.append("page"+QString::number(pageNum)); + baseString.append(apiSecret); + + // Create the url + QUrl url("http://ws.audioscrobbler.com/2.0/?"); + url.addQueryItem("album", album.name()); + url.addQueryItem("api_key", apiKey); + url.addQueryItem("limit", QString::number(limit)); + url.addQueryItem("page", QString::number(pageNum)); + url.addQueryItem("format", "json"); + url.addQueryItem("method", "album.search"); + url.addQueryItem("api_sig", generateSignature(baseString)); + + // Create the request, set the url + aRequest.iNetworkRequest.setUrl(url); + aRequest.iRequestType = SmfMusicSearchCustomRequest; + aRequest.iPostData = NULL; + aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; + error = SmfPluginErrNone; + + qDebug()<<"Url string is : "<initialize(); + } + +/** + * Method to get the provider information + * @return Instance of SmfProviderBase + */ +SmfProviderBase* LastFmMusicSearchPlugin::getProviderInfo( ) + { + return m_provider; + } + +/** + * Method to get the result for a network request. + * @param aOperation The type of operation to be requested + * @param aTransportResult The result of transport operation + * @param aResponse The QByteArray instance containing the network response. + * The plugins should delete this instance once they have read the + * data from it. + * @param aResult [out] An output parameter to the plugin manager.If the + * return value is SmfSendRequestAgain, QVariant will be of type + * SmfPluginRequestData. + * For SmfMusicSearchPlugin: If last operation was recommendations(), + * tracksSimilar() or tracksOfAlbum() or tracksOfArtist() or trackInfo(), + * aResult will be of type QList. + * If last operation was stores(), aResult will be of type QList. + * @param aRetType [out] SmfPluginRetType + * @param aPageResult [out] The SmfResultPage structure variable + */ +SmfPluginError LastFmMusicSearchPlugin::responseAvailable( + const SmfRequestTypeID aOperation, + const SmfTransportResult &aTransportResult, + QByteArray *aResponse, + QVariant* aResult, + SmfPluginRetType &aRetType, + SmfResultPage &aPageResult ) + { + Q_UNUSED(aPageResult) + qDebug()<<"Inside LastFmMusicSearchPlugin::responseAvailable()"; + + SmfPluginError error = SmfPluginErrNetworkError; + + if( !aResponse || (0 == aResponse->size()) ) + { + qDebug()<<"Response is NULL or empty"; + aRetType = SmfRequestError; + return error; + } + + QByteArray response(*aResponse); + delete aResponse; + + QFile respFile("c://data//SmfMusicSearchPluginResponse.txt"); + if(!respFile.open(QIODevice::WriteOnly)) + qDebug()<<"File to write the response could not be opened"; + else + { + respFile.write(response); + respFile.close(); + qDebug()<<"Writing FB response to a file named 'SmfMusicSearchPluginResponse.txt'"; + } + qDebug()<<"FB response size = "< list; + QString errStr; + errStr.clear(); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1; + if(SmfMusicGetTracksSimilar == aOperation) + map1 = result["similartracks"].toMap(); + else //if(SmfMusicGetTracksOfArtist == aOperation) // tracks of artists + map1 = result["toptracks"].toMap(); + QList list1 = map1["track"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfTrackInfo track; + + QVariantMap map2 = iter.next().toMap(); + qDebug()<<"track title = "< list2 = map3["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map4 = iter.next().toMap(); + + // Set the artist's image url + QUrl url(map4["#text"].toString()); + //artists.setImageUrlurl); + break; + } + + track.setArtists(artists); + + list.append(track); + if(gItemsPerPage == list.count()) + break; + } + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"list count = "<setValue(list); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + } + else if (SmfMusicGetTracksOfAlbum == aOperation) + { + qDebug()<<"Response for music search tracks of album"; + + if(0 == forTracksOfAlbum) // for fetching album ID + { + qDebug()<<"FOR GETTING ALBUMID"; + QString errStr; + errStr.clear(); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + qDebug()<<"Json parsing complete"; + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["album"].toMap(); + albumId.clear(); + albumId.append(map1["id"].toString()); + forTracksOfAlbum = 1; + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"album id = "< list; + QString errStr; + errStr.clear(); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["playlist"].toMap(); + QVariantMap map2 = map1["trackList"].toMap(); + QList list1 = map2["track"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfTrackInfo track; + + QVariantMap map3 = iter.next().toMap(); + qDebug()<<"track title = "< list; + QString errStr; + errStr.clear(); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + qDebug()<<"Json parsing complete"; + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["affiliations"].toMap(); + QVariantMap map2 = map1["physicals"].toMap(); + QList list1 = map2["affiliation"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfProvider provider; + + QVariantMap map3 = iter.next().toMap(); + qDebug()<<"provider name = "< list; + QString errStr; + errStr.clear(); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + qDebug()<<"Json parsing complete"; + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["tracks"].toMap(); + QList list1 = map1["track"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfTrackInfo track; + + QVariantMap map3 = iter.next().toMap(); + + // Set the track's Id - last.fm's identifier + track.setId(map3["mbid"].toString()); + + // Set the track title + track.setTitle(map3["name"].toString()); + + // Set the tracks artist details + QVariantMap map4 = map3["artist"].toMap(); + SmfArtists artists; + QStringList namesList; + namesList.append(map4["name"].toString()); + artists.setNames(namesList); + + QUrl url(map4["url"].toString()); + artists.setUrl(url); + + artists.setId(map4["mbid"].toString()); + + QList list2 = map4["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map5 = iter2.next().toMap(); + QUrl imageUrl(map5["#text"].toString()); + //artists.setImageUrl(imageUrl); + break; + } + + track.setArtists(artists); + + list.append(track); + + if(gItemsPerPage == list.count()) + break; + } + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"list count = "<setValue(list); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + } + + else if (SmfMusicSearchCustomRequest == aOperation) + { + qDebug()<<"Response for music search custom request = album.search"; + + QList list; + QString errStr; + errStr.clear(); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + qDebug()<<"Json parsing complete"; + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["results"].toMap(); + QVariantMap map2 = map1["albummatches"].toMap(); + QList list1 = map2["album"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfAlbum album; + + QVariantMap map3 = iter.next().toMap(); + + album.setName(map3["name"].toString()); + + SmfArtists artists; + QStringList names; + names.append(map3["artist"].toString()); + artists.setNames(names); + + QList list2 = map3["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map5 = iter2.next().toMap(); + QUrl imageUrl(map5["#text"].toString()); + //artists.setImageUrl(imageUrl); + break; + } + + album.setArtists(artists); + + album.setId(map3["id"].toString()); + + //QUrl url(map3["url"].toString()) + //album.setUrl(url); + + list.append(album); + + if(gItemsPerPage == list.count()) + break; + } + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + QByteArray customResponseData; + QByteArray entireData; + QDataStream writeResponse(&customResponseData, QIODevice::WriteOnly); + qDebug()<<"list count = "< LastFmMusicSearchProviderBase::supportedInterfaces( ) const + { + return m_supportedInterfaces; + } + +/** +* Method to get the list of languages supported by this service provider +* @return a QStringList of languages supported by this service +* provider in 2 letter ISO 639-1 format. +*/ +QStringList LastFmMusicSearchProviderBase::supportedLanguages( ) const + { + return m_supportedLangs; + } + +/** + * Method to get the Plugin specific ID + * @return The Plugin specific ID + */ +QString LastFmMusicSearchProviderBase::pluginId( ) const + { + return m_pluginId; + } + + +/** + * Method to get the ID of the authentication application + * for this service + * @param aProgram The authentication application name + * @param aArguments List of arguments required for authentication app + * @param aMode Strting mode for authentication application + * @return The ID of the authentication application + */ +QString LastFmMusicSearchProviderBase::authenticationApp( QString &aProgram, + QStringList & aArguments, + QIODevice::OpenModeFlag aMode ) const + { + Q_UNUSED(aProgram) + Q_UNUSED(aArguments) + Q_UNUSED(aMode) + return m_authAppId; + } + + +/** + * Method to get the unique registration ID provided by the + * Smf for authorised plugins + * @return The unique registration ID/token provided by the Smf for + * authorised plugins + */ +QString LastFmMusicSearchProviderBase::smfRegistrationId( ) const + { + return m_smfRegToken; + } + + +/** + * Method that initializes this class. This method should be called + * from the initialize() method of the FBContactFetcherPlugin class + */ +void LastFmMusicSearchProviderBase::initialize() + { + m_serviceName = "last.fm"; + m_description = "Last.fm music search plugin description"; + m_serviceUrl = QUrl(QString("http://www.last.fm")); + m_pluginId = "lastfmmusicsearchplugin.qtplugin"; + m_authAppId = "0x12345678"; + m_supportedInterfaces.append("org.symbian.smf.plugin.music.search/v0.2"); + QSettings iSettings; + m_smfRegToken = iSettings.value("LastFmRegToken").toString(); + m_validity = iSettings.value("LastFmExpiryTime").toDateTime(); + } + + +/* + * Export Macro + * plugin name : lastfmmusicsearchplugin + * plugin class : LastFmMusicSearchPlugin + */ +Q_EXPORT_PLUGIN2( lastfmmusicsearchplugin, LastFmMusicSearchPlugin )