diff -r b2eb79881f9d -r 574948b60dab example/lastfmplaylistserviceplugin/lastfmplaylistserviceplugin.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/lastfmplaylistserviceplugin/lastfmplaylistserviceplugin.cpp Thu Sep 23 17:15:03 2010 +0530 @@ -0,0 +1,918 @@ +/** + * 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: + * Sangeeta Prasad, Nalina Hariharan + * + * Description: + * The Plugin that does music services related functionalities from last.fm site + * + */ + +// Include files +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lastfmplaylistserviceplugin.h" + + +static int gPageNum = 0; +static int gItemsPerPage = 0; + +//Payload data array +QByteArray payload; + +/** + * Destructor + */ +LastFmPlaylistServicePlugin::~LastFmPlaylistServicePlugin( ) + { + 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 + * @param aName [out] The user name + */ +void LastFmPlaylistServicePlugin::fetchKeys( QString &aApiKey, + QString &aApiSecret, + QString &aToken, + QString &aName ) + { + 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)); + + keyName.clear(); + keyName.append("Name"); + aName.append(keys.value(keyName)); + + qDebug()<<"Api Key = "<().firstName().isEmpty() ) + { + qDebug()<<"Invalid arguments"; + return error; + } + + qDebug()<<"Valid arguments"; + + gPageNum = aPageNum; + gItemsPerPage = aItemsPerPage; + + // Get the key sets from SMF Plugin Utility class. + QString apiKey; + QString apiSecret; + QString token; + QString userName; + fetchKeys(apiKey, apiSecret, token, userName); + + userName.clear(); + userName = aUser.value("Name").value().firstName(); + + // Create the API signature string + QString baseString; + baseString.append("api_key"+apiKey); + baseString.append("methoduser.getPlaylists"); + baseString.append("user"+userName); + baseString.append(apiSecret); + + // Create the url + QUrl url("http://ws.audioscrobbler.com/2.0/?"); + url.addQueryItem("api_key", apiKey); + url.addQueryItem("format", "json"); + url.addQueryItem("method", "user.getPlaylists"); + url.addQueryItem("user", userName); + url.addQueryItem("api_sig", generateSignature(baseString)); + + // Create the request, set the url + aRequest.iNetworkRequest.setUrl(url); + aRequest.iRequestType = SmfMusicGetPlaylistsOfUser; + aRequest.iPostData = NULL; + aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; + error = SmfPluginErrNone; + + qDebug()<<"Url string is : "< &aTracks ) + { + qDebug()<<"Inside LastFmPlaylistServicePlugin::addToPlaylist()"; + + SmfPluginError error = SmfPluginErrInvalidArguments; + + // invalid arguments + if( aPlaylist.id().isEmpty() || (0 == aTracks.count()) ) + { + qDebug()<<"Invalid arguments"; + return error; + } + else if(aTracks.at(0).title().isEmpty() || aTracks.at(0).artists().names().at(0).isEmpty()) + { + qDebug()<<"Invalid arguments"; + return error; + } + else + qDebug()<<"Valid arguments"; + + + // Get the key sets from SMF Plugin Utility class. + QString apiKey; + QString apiSecret; + QString token; + QString userName; + fetchKeys(apiKey, apiSecret, token, userName); + + // Create the API signature string + QString baseString; + baseString.append("api_key"+apiKey); + baseString.append("artist"+aTracks.at(0).artists().names().at(0)); + baseString.append("methodplaylist.addTrack"); + baseString.append("playlistID"+aPlaylist.id()); + baseString.append("sk"+token); + baseString.append("track"+aTracks.at(0).title()); + baseString.append(apiSecret); + + QUrl postData; + postData.addQueryItem("api_key", apiKey); + postData.addQueryItem("artist", aTracks.at(0).artists().names().at(0)); + postData.addQueryItem("format","json"); + postData.addQueryItem("method", "playlist.addTrack"); + postData.addQueryItem("playlistID",aPlaylist.id()); + postData.addQueryItem("sk",token); + postData.addQueryItem("track",aTracks.at(0).title()); + postData.addQueryItem("api_sig", generateSignature(baseString)); + + QString data(postData.toString()); + data.remove(0, 1); // Remove the first ? + + payload.clear(); + payload.append(data.toAscii()); + + qDebug()<<"Data = "<initialize(); + } + +/** + * Method to get the provider information + * @return Instance of SmfProviderBase + */ +SmfProviderBase* LastFmPlaylistServicePlugin::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 SmfMusicServicePlugin: If last operation was userMusicInfo(), aResult + * will be of type SmfMusicProfile. If last operation was searchArtist(), + * aResult will be of type QList. If last operation was searchAlbum(), + * aResult will be of type QList. If last operation was searchEvents(), + * aResult will be of type QList. If last operation was searchVenue(), + * aResult will be of type QList. If last operation was searchUser(), + * aResult will be of type QList. If last operation was + * postCurrentPlaying() or postRating() or postComments(), aResult will be of + * type bool. + * @param aRetType [out] SmfPluginRetType + * @param aPageResult [out] The SmfResultPage structure variable + */ +SmfPluginError LastFmPlaylistServicePlugin::responseAvailable( + const SmfRequestTypeID aOperation, + const SmfTransportResult &aTransportResult, + QByteArray *aResponse, + QVariant* aResult, + SmfPluginRetType &aRetType, + SmfResultPage &aPageResult ) + { + + Q_UNUSED(aPageResult) + qDebug()<<"Inside LastFmPlaylistServicePlugin::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//SmfMusicPlaylistPluginResponse.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 'SmfMusicPlaylistPluginResponse.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; + } + + qDebug()<<"Json parsing complete"; + + if(response.contains(QByteArray("error"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["playlists"].toMap(); + QList list1 = map1["playlist"].toList(); + QListIterator iter1(list1); + bool ifList = false; + while(iter1.hasNext()) + { + ifList = true; + QVariantMap map2 = iter1.next().toMap(); + SmfPlaylist playlist; + + playlist.setPlayListTitle(map2["title"].toString()); + playlist.setId(map2["id"].toString()); + playlist.setAuthor(map2["creator"].toString()); + QUrl url(map2["url"].toString()); + playlist.setInfo(url); + playlist.setLocation(url); + + + QList list2 = map2["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map3 = iter2.next().toMap(); + QUrl imageUrl(map3["#text"].toString()); + playlist.setImage(imageUrl); + } + + list.append((playlist)); + + if(gItemsPerPage == list.count()) + break; + } + if(!ifList) + { + QVariantMap map2 = map1["playlist"].toMap(); + SmfPlaylist playlist; + + playlist.setPlayListTitle(map2["title"].toString()); + playlist.setId(map2["id"].toString()); + playlist.setAuthor(map2["creator"].toString()); + QUrl url(map2["url"].toString()); + playlist.setInfo(url); + playlist.setLocation(url); + + + QList list2 = map2["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map3 = iter2.next().toMap(); + QUrl imageUrl(map3["#text"].toString()); + playlist.setImage(imageUrl); + } + + list.append((playlist)); + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"Count of user's recent tracks = "<setValue(list); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + } + } + + // Playlists of another user + else if ( SmfMusicGetPlaylistsOfUser == aOperation ) + { + qDebug()<<"Response for getting another user's playlist"; + 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["playlists"].toMap(); + QList list1 = map1["playlist"].toList(); + QListIterator iter1(list1); + bool ifList = false; + while(iter1.hasNext()) + { + ifList = true; + QVariantMap map2 = iter1.next().toMap(); + SmfPlaylist playlist; + + playlist.setPlayListTitle(map2["title"].toString()); + playlist.setId(map2["id"].toString()); + playlist.setAuthor(map2["creator"].toString()); + QUrl url(map2["url"].toString()); + playlist.setInfo(url); + playlist.setLocation(url); + + + QList list2 = map2["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map3 = iter2.next().toMap(); + QUrl imageUrl(map3["#text"].toString()); + playlist.setImage(imageUrl); + } + + list.append((playlist)); + + if(gItemsPerPage == list.count()) + break; + } + if(!ifList) + { + QVariantMap map2 = map1["playlist"].toMap(); + SmfPlaylist playlist; + + playlist.setPlayListTitle(map2["title"].toString()); + playlist.setId(map2["id"].toString()); + playlist.setAuthor(map2["creator"].toString()); + QUrl url(map2["url"].toString()); + playlist.setInfo(url); + playlist.setLocation(url); + + + QList list2 = map2["image"].toList(); + QListIterator iter2(list2); + while(iter2.hasNext()) + { + QVariantMap map3 = iter2.next().toMap(); + QUrl imageUrl(map3["#text"].toString()); + playlist.setImage(imageUrl); + } + + list.append((playlist)); + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"Count of friend's playlists = "<setValue(list); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + } + } + + // Adding track to playlist AND + // Post current playing playlist + else if ((SmfMusicAddToPlaylist == aOperation) || (SmfMusicPostCurrentPlayingPlaylist == aOperation)) + { + qDebug()<<"Response for adding track to playlist"; + bool reqSuccess = false; + 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 + { + reqSuccess = true; + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"Track added to playlist?? "<setValue(reqSuccess); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + } + + // Other Service requests - NOT SUPPORTED + else + { + qDebug()<<"Service unsupported!!!"; + aRetType = SmfRequestError; + error = SmfPluginErrServiceNotSupported; + } + } + + else if(SmfTransportOpOperationCanceledError == aTransportResult) + { + qDebug()<<"Operation Cancelled !!!"; + error = SmfPluginErrCancelComplete; + aRetType = SmfRequestComplete; + } + + else + { + qDebug()<<"Transport Error !!!"; + error = SmfPluginErrNetworkError; + aRetType = SmfRequestError; + } + + return error; + + } + + + +/** + * Destructor + */ +LastFmPlaylistServiceProviderBase::~LastFmPlaylistServiceProviderBase( ) + { + } + +/** + * Method to get the Localisable name of the service. + * @return The Localisable name of the service. + */ +QString LastFmPlaylistServiceProviderBase::serviceName( ) const + { + return m_serviceName; + } + + +/** + * Method to get the Logo of the service + * @return The Logo of the service + */ +QImage LastFmPlaylistServiceProviderBase::serviceIcon( ) const + { + return m_serviceIcon; + } + + +/** + * Method to get the Readable service description + * @return The Readable service description + */ +QString LastFmPlaylistServiceProviderBase::description( ) const + { + return m_description; + } + + +/** + * Method to get the Website of the service + * @return The Website of the service + */ +QUrl LastFmPlaylistServiceProviderBase::serviceUrl( ) const + { + return m_serviceUrl; + } + + +/** + * Method to get the URL of the Application providing this service + * @return The URL of the Application providing this service + */ +QUrl LastFmPlaylistServiceProviderBase::applicationUrl( ) const + { + return m_applicationUrl; + } + + +/** + * Method to get the Icon of the application + * @return The Icon of the application + */ +QImage LastFmPlaylistServiceProviderBase::applicationIcon( ) const + { + return m_applicationIcon; + } + +/** +* Method to get the list of interfaces that this provider support +* @return List of supported Interafces +*/ +QList LastFmPlaylistServiceProviderBase::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 LastFmPlaylistServiceProviderBase::supportedLanguages( ) const + { + return m_supportedLangs; + } + +/** + * Method to get the Plugin specific ID + * @return The Plugin specific ID + */ +QString LastFmPlaylistServiceProviderBase::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 LastFmPlaylistServiceProviderBase::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 LastFmPlaylistServiceProviderBase::smfRegistrationId( ) const + { + return m_smfRegToken; + } + + +/** + * Method that initializes this class. This method should be called + * from the initialize() method of the FBContactFetcherPlugin class + */ +void LastFmPlaylistServiceProviderBase::initialize() + { + m_serviceName = "last.fm"; + m_description = "Last.fm music playlist plugin description"; + m_serviceUrl = QUrl(QString("http://www.last.fm")); + m_pluginId = "lastfmplaylistserviceplugin.qtplugin"; + m_authAppId = "0x12345678"; + m_supportedInterfaces.append("org.symbian.smf.plugin.music.playlist/v0.2"); + QSettings iSettings; + m_smfRegToken = iSettings.value("LastFmRegToken").toString(); + m_validity = iSettings.value("LastFmExpiryTime").toDateTime(); + } + + +/* + * Export Macro + * plugin name : lastfmplaylistserviceplugin + * plugin class : LastFmPlaylistServicePlugin + */ +Q_EXPORT_PLUGIN2( lastfmplaylistserviceplugin, LastFmPlaylistServicePlugin )