example/lastfmplaylistserviceplugin/lastfmplaylistserviceplugin.cpp
changeset 23 574948b60dab
child 26 83d6a149c755
equal deleted inserted replaced
22:b2eb79881f9d 23:574948b60dab
       
     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  * Sangeeta Prasad, Nalina Hariharan
       
    14  *
       
    15  * Description:
       
    16  * The Plugin that does music services related functionalities from last.fm site
       
    17  *
       
    18  */
       
    19 
       
    20 // Include files
       
    21 #include <QtPlugin>
       
    22 #include <QDebug>
       
    23 #include <QCryptographicHash>
       
    24 #include <QTextStream>
       
    25 #include <QFile>
       
    26 #include <QMap>
       
    27 #include <QListIterator>
       
    28 #include <QSettings>
       
    29 #include <smfpluginutil.h>
       
    30 
       
    31 #include "lastfmplaylistserviceplugin.h"
       
    32 
       
    33 
       
    34 static int gPageNum = 0; 
       
    35 static int gItemsPerPage = 0;
       
    36 
       
    37 //Payload data array
       
    38 QByteArray payload;
       
    39 
       
    40 /**
       
    41  * Destructor
       
    42  */
       
    43 LastFmPlaylistServicePlugin::~LastFmPlaylistServicePlugin( )
       
    44 	{
       
    45 	if(m_provider)
       
    46 		delete m_provider;
       
    47 	}
       
    48 
       
    49 /**
       
    50  * Method to interpret the key sets obtained from credential manager 
       
    51  * @param aApiKey [out] The api key
       
    52  * @param aApiSecret [out] The api secret
       
    53  * @param aSessionKey [out] The session key
       
    54  * @param aToken [out] The session token
       
    55  * @param aName [out] The user name
       
    56  */
       
    57 void LastFmPlaylistServicePlugin::fetchKeys( QString &aApiKey, 
       
    58 		QString &aApiSecret, 
       
    59 		QString &aToken,
       
    60 		QString &aName )
       
    61 	{
       
    62 	qDebug()<<"Reg Token = "<<m_provider->m_smfRegToken;
       
    63 	qDebug()<<"Expiry Date as int = "<<m_provider->m_validity.toTime_t();
       
    64 	
       
    65 	SmfAuthParams keys;
       
    66 	SmfPluginUtil util;
       
    67 	util.getAuthKeys(keys, m_provider->m_smfRegToken, 
       
    68 			m_provider->m_validity, m_provider->m_pluginId);
       
    69 	
       
    70     QByteArray keyName;
       
    71     keyName.append("ApiKey");
       
    72 	aApiKey.append(keys.value(keyName));
       
    73 	
       
    74     keyName.clear();
       
    75     keyName.append("ApiSecret");
       
    76 	aApiSecret.append(keys.value(keyName));
       
    77 	
       
    78 	keyName.clear();
       
    79     keyName.append("Token");
       
    80 	aToken.append(keys.value(keyName));
       
    81 	
       
    82 	keyName.clear();
       
    83     keyName.append("Name");
       
    84 	aName.append(keys.value(keyName));
       
    85 	
       
    86 	qDebug()<<"Api Key = "<<aApiKey;
       
    87 	qDebug()<<"Api Secret = "<<aApiSecret;
       
    88 	qDebug()<<"Token = "<<aToken;
       
    89 	qDebug()<<"Name = "<<aName;
       
    90 	}
       
    91 
       
    92 /**
       
    93  * Method called by plugins to generate a signature string from a base string
       
    94  * @param aBaseString The base string
       
    95  * @return The md5 hash of the base string
       
    96  */
       
    97 QString LastFmPlaylistServicePlugin::generateSignature(const QString aBaseString)
       
    98 	{
       
    99 	// Create md5 hash of the signature string
       
   100     QByteArray byteArray;
       
   101     byteArray.insert(0, aBaseString.toAscii());
       
   102 
       
   103     QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex();
       
   104     QString returnString (md5Hash);
       
   105     return returnString;
       
   106 	}
       
   107 
       
   108 /**
       
   109  * Method to get the playlist
       
   110  * @param aRequest [out] The request data to be sent to network
       
   111  * @param aPageNum [in] The page to be extracted
       
   112  * @param aItemsPerPage [in] Number of items per page
       
   113  * @return Appropriate value of the enum SmfPluginError.
       
   114  * Plugin error if any, else SmfPluginErrNone for success
       
   115  */
       
   116 SmfPluginError LastFmPlaylistServicePlugin::playlists( 
       
   117 		SmfPluginRequestData &aRequest,
       
   118 		const int aPageNum,
       
   119 		const int aItemsPerPage )
       
   120 	{
       
   121 	qDebug()<<"Inside LastFmPlaylistServicePlugin::playlists()";
       
   122 				
       
   123 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   124 
       
   125 	// invalid arguments
       
   126 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
   127 		{
       
   128 		qDebug()<<"Invalid arguments";
       
   129 		return error;
       
   130 		}
       
   131 	
       
   132 	qDebug()<<"Valid arguments";
       
   133 	
       
   134 	gPageNum = aPageNum;
       
   135 	gItemsPerPage = aItemsPerPage;
       
   136 
       
   137 	// Get the key sets from SMF Plugin Utility class.
       
   138 	QString apiKey;
       
   139 	QString apiSecret;
       
   140 	QString token;
       
   141 	QString userName;
       
   142 	fetchKeys(apiKey, apiSecret, token, userName);
       
   143 
       
   144 	// Create the API signature string
       
   145 	QString baseString;
       
   146 	baseString.append("api_key"+apiKey);
       
   147 	baseString.append("methoduser.getPlaylists");
       
   148 	baseString.append("user"+userName);
       
   149 	baseString.append(apiSecret);
       
   150 
       
   151 	// Create the url
       
   152 	QUrl url("http://ws.audioscrobbler.com/2.0/?");
       
   153 	url.addQueryItem("api_key", apiKey);
       
   154 	url.addQueryItem("format", "json");
       
   155 	url.addQueryItem("method", "user.getPlaylists");
       
   156 	url.addQueryItem("user", userName);
       
   157 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   158 	
       
   159 	// Create the request, set the url
       
   160 	aRequest.iNetworkRequest.setUrl(url);
       
   161 	aRequest.iRequestType = SmfMusicGetPlaylists;
       
   162 	aRequest.iPostData = NULL;
       
   163 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   164 	error = SmfPluginErrNone;
       
   165 	
       
   166 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   167 	return error;
       
   168 	}
       
   169 
       
   170 /**
       
   171  * Method to get the playlist of a particular user
       
   172  * @param aRequest [out] The request data to be sent to network
       
   173  * @param aUser [in] The user whose playlists need to be fetched
       
   174  * @param aPageNum [in] The page to be extracted
       
   175  * @param aItemsPerPage [in] Number of items per page
       
   176  * @return Appropriate value of the enum SmfPluginError.
       
   177  * Plugin error if any, else SmfPluginErrNone for success
       
   178  */
       
   179 SmfPluginError LastFmPlaylistServicePlugin::playlistsOf( 
       
   180 		SmfPluginRequestData &aRequest,
       
   181 		const SmfContact &aUser,
       
   182 		const int aPageNum, 
       
   183 		const int aItemsPerPage )
       
   184 	{
       
   185 	qDebug()<<"Inside LastFmPlaylistServicePlugin::playlistsOf()";
       
   186 					
       
   187 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   188 
       
   189 	// invalid arguments
       
   190 	if( aPageNum < 0 || aItemsPerPage < 0 || 
       
   191 			aUser.value("Name").value<QContactName>().firstName().isEmpty() )
       
   192 		{
       
   193 		qDebug()<<"Invalid arguments";
       
   194 		return error;
       
   195 		}
       
   196 	
       
   197 	qDebug()<<"Valid arguments";
       
   198 	
       
   199 	gPageNum = aPageNum;
       
   200 	gItemsPerPage = aItemsPerPage;
       
   201 	
       
   202 	// Get the key sets from SMF Plugin Utility class.
       
   203 	QString apiKey;
       
   204 	QString apiSecret;
       
   205 	QString token;
       
   206 	QString userName;
       
   207 	fetchKeys(apiKey, apiSecret, token, userName);
       
   208 	
       
   209 	userName.clear();
       
   210 	userName = aUser.value("Name").value<QContactName>().firstName();
       
   211 		
       
   212 	// Create the API signature string
       
   213 	QString baseString;
       
   214 	baseString.append("api_key"+apiKey);
       
   215 	baseString.append("methoduser.getPlaylists");
       
   216 	baseString.append("user"+userName);
       
   217 	baseString.append(apiSecret);
       
   218 
       
   219 	// Create the url
       
   220 	QUrl url("http://ws.audioscrobbler.com/2.0/?");
       
   221 	url.addQueryItem("api_key", apiKey);
       
   222 	url.addQueryItem("format", "json");
       
   223 	url.addQueryItem("method", "user.getPlaylists");
       
   224 	url.addQueryItem("user", userName);
       
   225 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   226 	
       
   227 	// Create the request, set the url
       
   228 	aRequest.iNetworkRequest.setUrl(url);
       
   229 	aRequest.iRequestType = SmfMusicGetPlaylistsOfUser;
       
   230 	aRequest.iPostData = NULL;
       
   231 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   232 	error = SmfPluginErrNone;
       
   233 
       
   234 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   235 	return error;
       
   236 	}
       
   237 	
       
   238 /**
       
   239  * Method to add tracks to a playlist
       
   240  * @param aRequest [out] The request data to be sent to network
       
   241  * @param aPlaylist [in] The playlist where tracks should be added
       
   242  * @param aTracks [in] The tracks to be added to the playlist
       
   243  * @return Appropriate value of the enum SmfPluginError.
       
   244  * Plugin error if any, else SmfPluginErrNone for success
       
   245  */
       
   246 SmfPluginError LastFmPlaylistServicePlugin::addToPlaylist( 
       
   247 		SmfPluginRequestData &aRequest,
       
   248 		const SmfPlaylist &aPlaylist, 
       
   249 		const QList<SmfTrackInfo> &aTracks )
       
   250 	{
       
   251 	qDebug()<<"Inside LastFmPlaylistServicePlugin::addToPlaylist()";
       
   252 					
       
   253 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   254 
       
   255 	// invalid arguments
       
   256 	if( aPlaylist.id().isEmpty() || (0 == aTracks.count()) )
       
   257 		{
       
   258 		qDebug()<<"Invalid arguments";
       
   259 		return error;
       
   260 		}
       
   261 	else if(aTracks.at(0).title().isEmpty() || aTracks.at(0).artists().names().at(0).isEmpty())
       
   262 		{
       
   263 		qDebug()<<"Invalid arguments";
       
   264 		return error;
       
   265 		}
       
   266 	else
       
   267 		qDebug()<<"Valid arguments";
       
   268 	
       
   269 
       
   270 	// Get the key sets from SMF Plugin Utility class.
       
   271 	QString apiKey;
       
   272 	QString apiSecret;
       
   273 	QString token;
       
   274 	QString userName;
       
   275 	fetchKeys(apiKey, apiSecret, token, userName);
       
   276 		
       
   277 	// Create the API signature string
       
   278 	QString baseString;
       
   279 	baseString.append("api_key"+apiKey);
       
   280 	baseString.append("artist"+aTracks.at(0).artists().names().at(0));
       
   281 	baseString.append("methodplaylist.addTrack");
       
   282 	baseString.append("playlistID"+aPlaylist.id());
       
   283 	baseString.append("sk"+token);
       
   284 	baseString.append("track"+aTracks.at(0).title());
       
   285 	baseString.append(apiSecret);
       
   286 
       
   287 	QUrl postData;
       
   288 	postData.addQueryItem("api_key", apiKey);
       
   289 	postData.addQueryItem("artist", aTracks.at(0).artists().names().at(0));
       
   290 	postData.addQueryItem("format","json");
       
   291 	postData.addQueryItem("method", "playlist.addTrack");
       
   292 	postData.addQueryItem("playlistID",aPlaylist.id());
       
   293 	postData.addQueryItem("sk",token);
       
   294 	postData.addQueryItem("track",aTracks.at(0).title());
       
   295 	postData.addQueryItem("api_sig", generateSignature(baseString));
       
   296 		
       
   297 	QString data(postData.toString());
       
   298 	data.remove(0, 1); // Remove the first ?
       
   299 			
       
   300 	payload.clear();
       
   301 	payload.append(data.toAscii());
       
   302 	
       
   303 	qDebug()<<"Data = "<<QString(payload);
       
   304 	
       
   305 	// Create the url
       
   306 	QUrl url("http://ws.audioscrobbler.com/2.0/");
       
   307 	
       
   308 	// Create the request, set the url
       
   309 	aRequest.iNetworkRequest.setUrl(url);
       
   310 	aRequest.iNetworkRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
       
   311 	aRequest.iRequestType = SmfMusicAddToPlaylist;
       
   312 	aRequest.iPostData = new QBuffer(&payload);
       
   313 	aRequest.iHttpOperationType = QNetworkAccessManager::PostOperation;
       
   314 	error = SmfPluginErrNone;
       
   315 
       
   316 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   317 	return error;
       
   318 	}
       
   319 	
       
   320 /**
       
   321  * Method to post the current playing playlist
       
   322  * @param aRequest [out] The request data to be sent to network
       
   323  * @param aPlaylist [in] The current playing playlist which should be posted
       
   324  * @return Appropriate value of the enum SmfPluginError.
       
   325  * Plugin error if any, else SmfPluginErrNone for success
       
   326  */
       
   327 SmfPluginError LastFmPlaylistServicePlugin::postCurrentPlayingPlaylist(
       
   328 		SmfPluginRequestData &aRequest, 
       
   329 		const SmfPlaylist &aPlaylist )
       
   330 	{
       
   331 	qDebug()<<"Inside LastFmPlaylistServicePlugin::postCurrentPlayingPlaylist()";
       
   332 					
       
   333 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   334 
       
   335 	// invalid arguments
       
   336 /*	if( aPlaylist.playListTitle().isEmpty() )
       
   337 		{
       
   338 		qDebug()<<"Invalid arguments";
       
   339 		return error;
       
   340 		}*/
       
   341 
       
   342 	qDebug()<<"Valid arguments";
       
   343 	
       
   344 	// Get the key sets from SMF Plugin Utility class.
       
   345 	QString apiKey;
       
   346 	QString apiSecret;
       
   347 	QString token;
       
   348 	QString userName;
       
   349 	fetchKeys(apiKey, apiSecret, token, userName);
       
   350 		
       
   351 	// Create the API signature string
       
   352 	QString baseString;
       
   353 	baseString.append("api_key"+apiKey);
       
   354 	baseString.append("methodplaylist.create");
       
   355 	baseString.append("sk"+token);
       
   356 	if( !aPlaylist.playListTitle().isEmpty()) //you can create a playlist without name also
       
   357 		baseString.append("title"+aPlaylist.playListTitle());
       
   358 	baseString.append(apiSecret);
       
   359 
       
   360 	QUrl postData;
       
   361 	postData.addQueryItem("api_key", apiKey);
       
   362 	postData.addQueryItem("format","json");
       
   363 	postData.addQueryItem("method", "playlist.create");
       
   364 	postData.addQueryItem("sk",token);
       
   365 	if( !aPlaylist.playListTitle().isEmpty())
       
   366 		postData.addQueryItem("title",aPlaylist.playListTitle());
       
   367 	postData.addQueryItem("api_sig", generateSignature(baseString));
       
   368 		
       
   369 	QString data(postData.toString());
       
   370 	data.remove(0, 1);
       
   371 			
       
   372 	payload.clear();
       
   373 	payload.append(data.toAscii());
       
   374 	
       
   375 	qDebug()<<"Data = "<<QString(payload);
       
   376 	
       
   377 	// Create the url
       
   378 	QUrl url("http://ws.audioscrobbler.com/2.0/");
       
   379 	
       
   380 	// Create the request, set the url
       
   381 	aRequest.iNetworkRequest.setUrl(url);
       
   382 	aRequest.iNetworkRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
       
   383 	aRequest.iRequestType = SmfMusicPostCurrentPlayingPlaylist;
       
   384 	aRequest.iPostData = new QBuffer(&payload);
       
   385 	aRequest.iHttpOperationType = QNetworkAccessManager::PostOperation;
       
   386 	error = SmfPluginErrNone;
       
   387 
       
   388 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   389 	return error;
       
   390 	}
       
   391 	
       
   392 /**
       
   393  * Customised method for SmfPlaylistServicePlugin interface
       
   394  * @param aRequest [out] The request data to be sent to network
       
   395  * @param aOperation [in] The operation type (should be known between 
       
   396  * the client interface and the plugin)
       
   397  * @param aData [in] The data required to form the request (The type 
       
   398  * of data should be known between client and the plugin)
       
   399  * @return Appropriate value of the enum SmfPluginError.
       
   400  * Plugin error if any, else SmfPluginErrNone for success
       
   401  */
       
   402 SmfPluginError LastFmPlaylistServicePlugin::customRequest( SmfPluginRequestData &aRequest, 
       
   403 		const int &aOperation, QByteArray *aData )
       
   404 	{
       
   405 	Q_UNUSED(aRequest)
       
   406 	Q_UNUSED(aOperation)
       
   407 	Q_UNUSED(aData)
       
   408 	qDebug()<<"Inside LastFmPlaylistServicePlugin::customRequest()";
       
   409 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   410 	return error;
       
   411 	}
       
   412 	
       
   413 /**
       
   414  * The first method to be called in the plugin that implements this interface.
       
   415  * If this method is not called, plugin may not behave as expected.
       
   416  */
       
   417 void LastFmPlaylistServicePlugin::initialize( )
       
   418 	{
       
   419 	// Create an instance of LastFmMusicServiceProviderBase
       
   420 	m_provider = new LastFmPlaylistServiceProviderBase;
       
   421 	m_provider->initialize();
       
   422 	}
       
   423 
       
   424 /**
       
   425  * Method to get the provider information
       
   426  * @return Instance of SmfProviderBase
       
   427  */
       
   428 SmfProviderBase* LastFmPlaylistServicePlugin::getProviderInfo( )
       
   429 	{
       
   430 	return m_provider;
       
   431 	}
       
   432 
       
   433 /**
       
   434  * Method to get the result for a network request.
       
   435  * @param aOperation The type of operation to be requested
       
   436  * @param aTransportResult The result of transport operation
       
   437  * @param aResponse The QByteArray instance containing the network response.
       
   438  * The plugins should delete this instance once they have read the
       
   439  * data from it.
       
   440  * @param aResult [out] An output parameter to the plugin manager.If the
       
   441  * return value is SmfSendRequestAgain, QVariant will be of type
       
   442  * SmfPluginRequestData.
       
   443  * For SmfMusicServicePlugin: If last operation was userMusicInfo(), aResult 
       
   444  * will be of type SmfMusicProfile. If last operation was searchArtist(), 
       
   445  * aResult will be of type QList<SmfArtists>. If last operation was searchAlbum(), 
       
   446  * aResult will be of type QList<SmfAlbum>. If last operation was searchEvents(), 
       
   447  * aResult will be of type QList<SmfEvent>. If last operation was searchVenue(), 
       
   448  * aResult will be of type QList<Smfocation>. If last operation was searchUser(), 
       
   449  * aResult will be of type QList<SmfMusicProfile>. If last operation was 
       
   450  * postCurrentPlaying() or postRating() or postComments(), aResult will be of 
       
   451  * type bool.
       
   452  * @param aRetType [out] SmfPluginRetType
       
   453  * @param aPageResult [out] The SmfResultPage structure variable
       
   454  */
       
   455 SmfPluginError LastFmPlaylistServicePlugin::responseAvailable(
       
   456 		const SmfRequestTypeID aOperation,
       
   457 		const SmfTransportResult &aTransportResult,
       
   458 		QByteArray *aResponse,
       
   459 		QVariant* aResult,
       
   460 		SmfPluginRetType &aRetType,
       
   461 		SmfResultPage &aPageResult )
       
   462 	{
       
   463 
       
   464 	Q_UNUSED(aPageResult)
       
   465 	qDebug()<<"Inside LastFmPlaylistServicePlugin::responseAvailable()";
       
   466 	
       
   467 	SmfPluginError error = SmfPluginErrNetworkError;
       
   468 	
       
   469 	if( !aResponse || (0 == aResponse->size()) )
       
   470 		{
       
   471 		qDebug()<<"Response is NULL or empty";
       
   472 		aRetType = SmfRequestError;
       
   473 		return error;
       
   474 		}
       
   475 	
       
   476 	QByteArray response(*aResponse);
       
   477 	delete aResponse;
       
   478 	
       
   479 	QFile respFile("c://data//SmfMusicPlaylistPluginResponse.txt");
       
   480 	if(!respFile.open(QIODevice::WriteOnly))
       
   481 		qDebug()<<"File to write the response could not be opened";
       
   482 	else
       
   483 		{
       
   484 		respFile.write(response);
       
   485 		respFile.close();
       
   486 		qDebug()<<"Writing FB response to a file named 'SmfMusicPlaylistPluginResponse.txt'";
       
   487 		}
       
   488 	qDebug()<<"FB response size = "<<response.size();
       
   489 	
       
   490 	if(SmfTransportOpNoError == aTransportResult)
       
   491 		{
       
   492 		qDebug()<<"No transport error";
       
   493 		
       
   494 		// Get user's playlists
       
   495 		if(SmfMusicGetPlaylists == aOperation)
       
   496 			{
       
   497 			qDebug()<<"Response for getting user's playlist";
       
   498 			QList<SmfPlaylist> list;				
       
   499 			QString errStr;
       
   500 			errStr.clear();
       
   501 				
       
   502 			bool ok;
       
   503 			SmfPluginUtil util;
       
   504 			QVariantMap result = util.parse(response, &ok).toMap();
       
   505 			if (!ok) 
       
   506 				{
       
   507 				qDebug()<<"An error occurred during json parsing";
       
   508 				aRetType = SmfRequestError;
       
   509 				return SmfPluginErrParsingFailed;
       
   510 				}
       
   511 					
       
   512 			qDebug()<<"Json parsing complete";
       
   513 			
       
   514 			if(response.contains(QByteArray("error")))
       
   515 				{
       
   516 				errStr.append(result["message"].toString());
       
   517 				}
       
   518 			else
       
   519 				{
       
   520 				QVariantMap map1 = result["playlists"].toMap();
       
   521 				QList<QVariant> list1 = map1["playlist"].toList();
       
   522 				QListIterator<QVariant> iter1(list1);
       
   523 				bool ifList = false;
       
   524 				while(iter1.hasNext())
       
   525 					{
       
   526 					ifList = true;
       
   527 					QVariantMap map2 = iter1.next().toMap();
       
   528 					SmfPlaylist playlist;
       
   529 					
       
   530 					playlist.setPlayListTitle(map2["title"].toString());
       
   531 					playlist.setId(map2["id"].toString());
       
   532 					playlist.setAuthor(map2["creator"].toString());
       
   533 					QUrl url(map2["url"].toString());
       
   534 					playlist.setInfo(url);
       
   535 					playlist.setLocation(url);
       
   536 					
       
   537 					
       
   538 					QList<QVariant> list2 = map2["image"].toList();
       
   539 					QListIterator<QVariant> iter2(list2);
       
   540 					while(iter2.hasNext())
       
   541 						{
       
   542 						QVariantMap map3 = iter2.next().toMap();
       
   543 						QUrl imageUrl(map3["#text"].toString());
       
   544 						playlist.setImage(imageUrl);
       
   545 						}
       
   546 					
       
   547 					list.append((playlist));
       
   548 					
       
   549 					if(gItemsPerPage == list.count())
       
   550 						break;
       
   551 					}
       
   552 				if(!ifList)
       
   553 					{
       
   554 					QVariantMap map2 = map1["playlist"].toMap();
       
   555 					SmfPlaylist playlist;
       
   556 					
       
   557 					playlist.setPlayListTitle(map2["title"].toString());
       
   558 					playlist.setId(map2["id"].toString());
       
   559 					playlist.setAuthor(map2["creator"].toString());
       
   560 					QUrl url(map2["url"].toString());
       
   561 					playlist.setInfo(url);
       
   562 					playlist.setLocation(url);
       
   563 					
       
   564 					
       
   565 					QList<QVariant> list2 = map2["image"].toList();
       
   566 					QListIterator<QVariant> iter2(list2);
       
   567 					while(iter2.hasNext())
       
   568 						{
       
   569 						QVariantMap map3 = iter2.next().toMap();
       
   570 						QUrl imageUrl(map3["#text"].toString());
       
   571 						playlist.setImage(imageUrl);
       
   572 						}
       
   573 					
       
   574 					list.append((playlist));
       
   575 					}
       
   576 					
       
   577 				if(errStr.size())
       
   578 					{
       
   579 					qDebug()<<"Response error found = "<<errStr;
       
   580 					error = SmfPluginErrInvalidRequest;
       
   581 					aRetType = SmfRequestError;
       
   582 					aResult->setValue(errStr);
       
   583 					}
       
   584 				else
       
   585 					{
       
   586 					qDebug()<<"Count of user's recent tracks  = "<<list.count();
       
   587 					aResult->setValue(list);
       
   588 					aRetType = SmfRequestComplete;
       
   589 					error = SmfPluginErrNone;
       
   590 					}
       
   591 				}
       
   592 			}
       
   593 		
       
   594 		// Playlists of another user
       
   595 		else if ( SmfMusicGetPlaylistsOfUser == aOperation )
       
   596 			{
       
   597 			qDebug()<<"Response for getting another user's playlist";
       
   598 			QList<SmfPlaylist> list;				
       
   599 			QString errStr;
       
   600 			errStr.clear();
       
   601 				
       
   602 			bool ok;
       
   603 			SmfPluginUtil util;
       
   604 			QVariantMap result = util.parse(response, &ok).toMap();
       
   605 			if (!ok) 
       
   606 				{
       
   607 				qDebug()<<"An error occurred during json parsing";
       
   608 				aRetType = SmfRequestError;
       
   609 				return SmfPluginErrParsingFailed;
       
   610 				}
       
   611 					
       
   612 			qDebug()<<"Json parsing complete";
       
   613 			
       
   614 			if(response.contains(QByteArray("error")))
       
   615 				{
       
   616 				errStr.append(result["message"].toString());
       
   617 				}
       
   618 			else
       
   619 				{
       
   620 				QVariantMap map1 = result["playlists"].toMap();
       
   621 				QList<QVariant> list1 = map1["playlist"].toList();
       
   622 				QListIterator<QVariant> iter1(list1);
       
   623 				bool ifList = false;
       
   624 				while(iter1.hasNext())
       
   625 					{
       
   626 					ifList = true;
       
   627 					QVariantMap map2 = iter1.next().toMap();
       
   628 					SmfPlaylist playlist;
       
   629 					
       
   630 					playlist.setPlayListTitle(map2["title"].toString());
       
   631 					playlist.setId(map2["id"].toString());
       
   632 					playlist.setAuthor(map2["creator"].toString());
       
   633 					QUrl url(map2["url"].toString());
       
   634 					playlist.setInfo(url);
       
   635 					playlist.setLocation(url);
       
   636 					
       
   637 					
       
   638 					QList<QVariant> list2 = map2["image"].toList();
       
   639 					QListIterator<QVariant> iter2(list2);
       
   640 					while(iter2.hasNext())
       
   641 						{
       
   642 						QVariantMap map3 = iter2.next().toMap();
       
   643 						QUrl imageUrl(map3["#text"].toString());
       
   644 						playlist.setImage(imageUrl);
       
   645 						}
       
   646 					
       
   647 					list.append((playlist));
       
   648 					
       
   649 					if(gItemsPerPage == list.count())
       
   650 						break;
       
   651 					}
       
   652 				if(!ifList)
       
   653 					{
       
   654 					QVariantMap map2 = map1["playlist"].toMap();
       
   655 					SmfPlaylist playlist;
       
   656 					
       
   657 					playlist.setPlayListTitle(map2["title"].toString());
       
   658 					playlist.setId(map2["id"].toString());
       
   659 					playlist.setAuthor(map2["creator"].toString());
       
   660 					QUrl url(map2["url"].toString());
       
   661 					playlist.setInfo(url);
       
   662 					playlist.setLocation(url);
       
   663 					
       
   664 					
       
   665 					QList<QVariant> list2 = map2["image"].toList();
       
   666 					QListIterator<QVariant> iter2(list2);
       
   667 					while(iter2.hasNext())
       
   668 						{
       
   669 						QVariantMap map3 = iter2.next().toMap();
       
   670 						QUrl imageUrl(map3["#text"].toString());
       
   671 						playlist.setImage(imageUrl);
       
   672 						}
       
   673 					
       
   674 					list.append((playlist));
       
   675 					}
       
   676 					
       
   677 				if(errStr.size())
       
   678 					{
       
   679 					qDebug()<<"Response error found = "<<errStr;
       
   680 					error = SmfPluginErrInvalidRequest;
       
   681 					aRetType = SmfRequestError;
       
   682 					aResult->setValue(errStr);
       
   683 					}
       
   684 				else
       
   685 					{
       
   686 					qDebug()<<"Count of friend's playlists  = "<<list.count();
       
   687 					aResult->setValue(list);
       
   688 					aRetType = SmfRequestComplete;
       
   689 					error = SmfPluginErrNone;
       
   690 					}
       
   691 				}
       
   692 			}
       
   693 		
       
   694 		// Adding track to playlist AND
       
   695 		// Post current playing playlist
       
   696 		else if ((SmfMusicAddToPlaylist == aOperation) || (SmfMusicPostCurrentPlayingPlaylist == aOperation))
       
   697 			{
       
   698 			qDebug()<<"Response for adding track to playlist";
       
   699 			bool reqSuccess = false;		
       
   700 			QString errStr;
       
   701 			errStr.clear();
       
   702 				
       
   703 			bool ok;
       
   704 			SmfPluginUtil util;
       
   705 			QVariantMap result = util.parse(response, &ok).toMap();
       
   706 			if (!ok) 
       
   707 				{
       
   708 				qDebug()<<"An error occurred during json parsing";
       
   709 				aRetType = SmfRequestError;
       
   710 				return SmfPluginErrParsingFailed;
       
   711 				}
       
   712 					
       
   713 			qDebug()<<"Json parsing complete";
       
   714 			
       
   715 			if(response.contains(QByteArray("error")))
       
   716 				{
       
   717 				errStr.append(result["message"].toString());
       
   718 				}
       
   719 			else
       
   720 				{
       
   721 				reqSuccess = true;
       
   722 				}
       
   723 					
       
   724 			if(errStr.size())
       
   725 				{
       
   726 				qDebug()<<"Response error found = "<<errStr;
       
   727 				error = SmfPluginErrInvalidRequest;
       
   728 				aRetType = SmfRequestError;
       
   729 				aResult->setValue(errStr);
       
   730 				}
       
   731 			else
       
   732 				{
       
   733 				qDebug()<<"Track added to playlist?? "<<reqSuccess;
       
   734 				aResult->setValue(reqSuccess);
       
   735 				aRetType = SmfRequestComplete;
       
   736 				error = SmfPluginErrNone;
       
   737 				}
       
   738 			}
       
   739 		
       
   740 		// Other Service requests - NOT SUPPORTED
       
   741 		else
       
   742 			{
       
   743 			qDebug()<<"Service unsupported!!!";
       
   744 			aRetType = SmfRequestError;
       
   745 			error = SmfPluginErrServiceNotSupported;
       
   746 			}
       
   747 		}
       
   748 
       
   749 		else if(SmfTransportOpOperationCanceledError == aTransportResult)
       
   750 			{
       
   751 			qDebug()<<"Operation Cancelled !!!";
       
   752 			error = SmfPluginErrCancelComplete;
       
   753 			aRetType = SmfRequestComplete;
       
   754 			}
       
   755 
       
   756 		else
       
   757 			{
       
   758 			qDebug()<<"Transport Error !!!";
       
   759 			error = SmfPluginErrNetworkError;
       
   760 			aRetType = SmfRequestError;
       
   761 			}
       
   762 		
       
   763 		return error;
       
   764 	
       
   765 	}
       
   766 
       
   767 
       
   768 
       
   769 /**
       
   770  * Destructor
       
   771  */
       
   772 LastFmPlaylistServiceProviderBase::~LastFmPlaylistServiceProviderBase( )
       
   773 	{
       
   774 	}
       
   775 
       
   776 /**
       
   777  * Method to get the Localisable name of the service.
       
   778  * @return The Localisable name of the service.
       
   779  */
       
   780 QString LastFmPlaylistServiceProviderBase::serviceName( ) const
       
   781 	{
       
   782 	return m_serviceName;
       
   783 	}
       
   784 
       
   785 
       
   786 /**
       
   787  * Method to get the Logo of the service
       
   788  * @return The Logo of the service
       
   789  */
       
   790 QImage LastFmPlaylistServiceProviderBase::serviceIcon( ) const
       
   791 	{
       
   792 	return m_serviceIcon;
       
   793 	}
       
   794 
       
   795 
       
   796 /**
       
   797  * Method to get the Readable service description
       
   798  * @return The Readable service description
       
   799  */
       
   800 QString LastFmPlaylistServiceProviderBase::description( ) const
       
   801 	{
       
   802 	return m_description;
       
   803 	}
       
   804 
       
   805 
       
   806 /**
       
   807  * Method to get the Website of the service
       
   808  * @return The Website of the service
       
   809  */
       
   810 QUrl LastFmPlaylistServiceProviderBase::serviceUrl( ) const
       
   811 	{
       
   812 	return m_serviceUrl;
       
   813 	}
       
   814 
       
   815 
       
   816 /**
       
   817  * Method to get the URL of the Application providing this service
       
   818  * @return The URL of the Application providing this service
       
   819  */
       
   820 QUrl LastFmPlaylistServiceProviderBase::applicationUrl( ) const
       
   821 	{
       
   822 	return m_applicationUrl;
       
   823 	}
       
   824 
       
   825 
       
   826 /**
       
   827  * Method to get the Icon of the application
       
   828  * @return The Icon of the application
       
   829  */
       
   830 QImage LastFmPlaylistServiceProviderBase::applicationIcon( ) const
       
   831 	{
       
   832 	return m_applicationIcon;
       
   833 	}
       
   834 
       
   835 /**
       
   836 * Method to get the list of interfaces that this provider support
       
   837 * @return List of supported Interafces
       
   838 */
       
   839 QList<QString> LastFmPlaylistServiceProviderBase::supportedInterfaces( ) const
       
   840 	{
       
   841 	return m_supportedInterfaces;
       
   842 	}
       
   843 
       
   844 /**
       
   845 * Method to get the list of languages supported by this service provider
       
   846 * @return a QStringList of languages supported by this service 
       
   847 * provider in 2 letter ISO 639-1 format.
       
   848 */
       
   849 QStringList LastFmPlaylistServiceProviderBase::supportedLanguages( ) const
       
   850 	{
       
   851 	return m_supportedLangs;
       
   852 	}
       
   853 
       
   854 /**
       
   855  * Method to get the Plugin specific ID
       
   856  * @return The Plugin specific ID
       
   857  */
       
   858 QString LastFmPlaylistServiceProviderBase::pluginId( ) const
       
   859 	{
       
   860 	return m_pluginId;
       
   861 	}
       
   862 
       
   863 
       
   864 /**
       
   865  * Method to get the ID of the authentication application 
       
   866  * for this service
       
   867  * @param aProgram The authentication application name
       
   868  * @param aArguments List of arguments required for authentication app
       
   869  * @param aMode Strting mode for authentication application
       
   870  * @return The ID of the authentication application 
       
   871  */
       
   872 QString LastFmPlaylistServiceProviderBase::authenticationApp( QString &aProgram, 
       
   873 		QStringList & aArguments, 
       
   874 		QIODevice::OpenModeFlag aMode ) const
       
   875 	{
       
   876 	Q_UNUSED(aProgram)
       
   877 	Q_UNUSED(aArguments)
       
   878 	Q_UNUSED(aMode)
       
   879 	return m_authAppId;
       
   880 	}
       
   881 
       
   882 
       
   883 /**
       
   884  * Method to get the unique registration ID provided by the 
       
   885  * Smf for authorised plugins
       
   886  * @return The unique registration ID/token provided by the Smf for 
       
   887  * authorised plugins
       
   888  */
       
   889 QString LastFmPlaylistServiceProviderBase::smfRegistrationId( ) const
       
   890 	{
       
   891 	return m_smfRegToken;
       
   892 	}
       
   893 
       
   894 
       
   895 /**
       
   896  * Method that initializes this class. This method should be called 
       
   897  * from the initialize() method of the FBContactFetcherPlugin class
       
   898  */
       
   899 void LastFmPlaylistServiceProviderBase::initialize()
       
   900 	{
       
   901 	m_serviceName = "last.fm";
       
   902 	m_description = "Last.fm music playlist plugin description";
       
   903 	m_serviceUrl = QUrl(QString("http://www.last.fm"));
       
   904 	m_pluginId = "lastfmplaylistserviceplugin.qtplugin";
       
   905 	m_authAppId = "0x12345678";
       
   906 	m_supportedInterfaces.append("org.symbian.smf.plugin.music.playlist/v0.2");
       
   907 	QSettings iSettings;
       
   908 	m_smfRegToken = iSettings.value("LastFmRegToken").toString();
       
   909 	m_validity = iSettings.value("LastFmExpiryTime").toDateTime();
       
   910 	}
       
   911 
       
   912 
       
   913 /*
       
   914  * Export Macro
       
   915  * plugin name : lastfmplaylistserviceplugin
       
   916  * plugin class : LastFmPlaylistServicePlugin
       
   917  */
       
   918 Q_EXPORT_PLUGIN2( lastfmplaylistserviceplugin, LastFmPlaylistServicePlugin )