example/fbcontactfetcherplugin/fbcontactfetcherplugin.cpp
changeset 14 a469c0e6e7fb
child 17 106a4bfcb866
equal deleted inserted replaced
13:b5d63d5fc252 14:a469c0e6e7fb
       
     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  * Nalina Hariharan
       
    14  * 
       
    15  * Description:
       
    16  * The Plugin that fetches contacts from the logged in user's facebook account
       
    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 #ifdef SMF_XMLPARSING
       
    29 #include <QXmlStreamReader>
       
    30 #endif
       
    31 
       
    32 #include "fbcontactfetcherplugin.h"
       
    33 
       
    34 // Global variables
       
    35 static int chance = 0;
       
    36 QString uids;
       
    37 
       
    38 #ifdef SMF_XMLPARSING
       
    39 SmfContact contact;
       
    40 #endif
       
    41 
       
    42 
       
    43 /**
       
    44  * Destructor
       
    45  */
       
    46 FBContactFetcherPlugin::~FBContactFetcherPlugin( )
       
    47 	{
       
    48 	if(m_provider)
       
    49 		delete m_provider;
       
    50 	}
       
    51 
       
    52 /**
       
    53  * Method to get the list of friends
       
    54  * @param aRequest [out] The request data to be sent to network
       
    55  * @param aPageNum The page to be extracted
       
    56  * @param aItemsPerPage Number of items per page
       
    57  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
    58  */
       
    59 SmfPluginError FBContactFetcherPlugin::friends( SmfPluginRequestData &aRequest,
       
    60 		const int aPageNum, 
       
    61 		const int aItemsPerPage )
       
    62 	{
       
    63 	qDebug()<<"Inside FBContactFetcherPlugin::friends()";
       
    64 	
       
    65 	if(0 == chance)
       
    66 		return getFriendsUids(aRequest, aPageNum, aItemsPerPage);
       
    67 	else// if(1 == chance)
       
    68 		return getFriendsDetails(aRequest, aPageNum, aItemsPerPage);
       
    69 
       
    70 	}
       
    71 
       
    72 /**
       
    73  * Method to get Facebook specific UIDs of the user's friends
       
    74  * @param aRequest [out] The request data to be sent to network
       
    75  * @param aPageNum The page to be extracted
       
    76  * @param aItemsPerPage Number of items per page
       
    77  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
    78  */
       
    79 SmfPluginError FBContactFetcherPlugin::getFriendsUids( SmfPluginRequestData &aRequest,
       
    80 		const int aPageNum, 
       
    81 		const int aItemsPerPage )
       
    82 	{
       
    83 	qDebug()<<"Inside FBContactFetcherPlugin::getFriendsUids()";
       
    84 	
       
    85 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
    86 
       
    87 	// invalid arguments
       
    88 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
    89 		{
       
    90 		qDebug()<<"Invalid arguments";
       
    91 		return error;
       
    92 		}
       
    93 	
       
    94 	qDebug()<<"Valid arguments";
       
    95 
       
    96 #if 1
       
    97 // Reading the keys, CSM Stubbed - START
       
    98 	QFile file("c:\\data\\FacebookKeys.txt");
       
    99 	if (!file.open(QIODevice::ReadOnly))
       
   100 		{
       
   101 		qDebug()<<"File to read the keys could not be opened";
       
   102 		return SmfPluginErrUserNotLoggedIn;
       
   103 		}
       
   104 	
       
   105 	qDebug()<<"Key file read, going to parse the key values from file";
       
   106 	
       
   107 	QByteArray arr = file.readAll();
       
   108 	QList<QByteArray> list = arr.split('\n');
       
   109 	file.close();
       
   110 	
       
   111 	QString apiKey(list[0]);
       
   112 	QString apiSecret(list[1]);
       
   113 	QString sessionKey(list[2]);
       
   114 	QString sessionSecret(list[3]);
       
   115 	
       
   116 	qDebug()<<"Api Key = "<<apiKey;
       
   117 	qDebug()<<"Api Secret = "<<apiSecret;
       
   118 	qDebug()<<"session Key = "<<sessionKey;
       
   119 	qDebug()<<"session Secret = "<<sessionSecret;
       
   120 // Reading the keys, CSM Stubbed - END
       
   121 #endif
       
   122 		
       
   123 	// Get the current date and time and convert it to seconds as a string
       
   124 	QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10);
       
   125 	
       
   126 	// Create the API signature string
       
   127 	QString baseString;
       
   128 	baseString.append("api_key="+apiKey);
       
   129 	baseString.append("call_id="+call_id);
       
   130 #ifdef SMF_XMLPARSING
       
   131 	baseString.append("format=XML");
       
   132 #else
       
   133 	baseString.append("format=JSON");
       
   134 #endif
       
   135 	if(0 == aPageNum)
       
   136 		baseString.append("limit="+QString::number((aItemsPerPage*(aPageNum+1)), 10));
       
   137 	else
       
   138 		baseString.append("limit="+QString::number((aItemsPerPage*aPageNum), 10));
       
   139 	baseString.append("method=friends.get");
       
   140 	baseString.append("session_key="+sessionKey);
       
   141 	baseString.append("ss=1");
       
   142 	baseString.append("v=1.0");
       
   143 	baseString.append(sessionSecret);
       
   144 
       
   145 	// Create the url
       
   146 	QUrl url("http://api.facebook.com/restserver.php?");
       
   147 	url.addQueryItem("api_key", apiKey);
       
   148 	url.addQueryItem("call_id", call_id);
       
   149 #ifdef SMF_XMLPARSING
       
   150 	url.addQueryItem("format", "XML");
       
   151 #else
       
   152 	url.addQueryItem("format", "JSON");
       
   153 #endif
       
   154 	if(0 == aPageNum)
       
   155 		url.addQueryItem("limit", QString::number((aItemsPerPage*(aPageNum+1)), 10));
       
   156 	else
       
   157 		url.addQueryItem("limit", QString::number((aItemsPerPage*aPageNum), 10));
       
   158 	url.addQueryItem("method", "friends.get");
       
   159 	url.addQueryItem("session_key", sessionKey);
       
   160 	url.addQueryItem("ss", "1");
       
   161 	url.addQueryItem("v", "1.0");		
       
   162 	url.addQueryItem("sig", generateSignature(baseString));
       
   163 			
       
   164 	// Create the request, set the url
       
   165 	aRequest.iNetworkRequest.setUrl(url);
       
   166 	aRequest.iRequestType = SmfContactGetFriends;
       
   167 	aRequest.iPostData = NULL;
       
   168 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   169 	error = SmfPluginErrNone;
       
   170 
       
   171 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   172 	return error;
       
   173 	}
       
   174 
       
   175 
       
   176 /**
       
   177  * Method to get the details of users friends from facebook
       
   178  * @param aRequest [out] The request data to be sent to network
       
   179  * @param aPageNum The page to be extracted
       
   180  * @param aItemsPerPage Number of items per page
       
   181  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   182  */
       
   183 SmfPluginError FBContactFetcherPlugin::getFriendsDetails( SmfPluginRequestData &aRequest,
       
   184 		const int aPageNum, 
       
   185 		const int aItemsPerPage )
       
   186 	{
       
   187 	Q_UNUSED(aPageNum)
       
   188 	Q_UNUSED(aItemsPerPage)
       
   189 	qDebug()<<"Inside FBContactFetcherPlugin::getFriendsDetails()";
       
   190 	
       
   191 	SmfPluginError error = SmfPluginErrUserNotLoggedIn;
       
   192 	
       
   193 #if 1
       
   194 // Reading the keys, CSM Stubbed - START
       
   195 	QFile file("c:\\data\\FacebookKeys.txt");
       
   196 	if (!file.open(QIODevice::ReadOnly))
       
   197 		{
       
   198 		qDebug()<<"File to read the keys could not be opened";
       
   199 		return error;
       
   200 		}
       
   201 	
       
   202 	qDebug()<<"Key file read, going to parse the key values from file";
       
   203 	
       
   204 	QByteArray arr = file.readAll();
       
   205 	QList<QByteArray> list = arr.split('\n');
       
   206 	file.close();
       
   207 	
       
   208 	QString apiKey(list[0]);
       
   209 	QString apiSecret(list[1]);
       
   210 	QString sessionKey(list[2]);
       
   211 	QString sessionSecret(list[3]);
       
   212 	
       
   213 	qDebug()<<"Api Key = "<<apiKey;
       
   214 	qDebug()<<"Api Secret = "<<apiSecret;
       
   215 	qDebug()<<"session Key = "<<sessionKey;
       
   216 	qDebug()<<"session Secret = "<<sessionSecret;
       
   217 // Reading the keys, CSM Stubbed - END
       
   218 #endif
       
   219 		
       
   220 	// Get the current date and time and convert it to sec as a string
       
   221 	QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10);
       
   222 	
       
   223 	// Create the API signature string
       
   224 	QString baseString;
       
   225 	baseString.append("api_key="+apiKey);
       
   226 	baseString.append("call_id="+call_id);
       
   227 	baseString.append("fields=uid,name,pic,pic_square");
       
   228 #ifdef SMF_XMLPARSING
       
   229 	baseString.append("format=XML");
       
   230 #else
       
   231 	baseString.append("format=JSON");
       
   232 #endif
       
   233 	baseString.append("method=users.getInfo");
       
   234 	baseString.append("session_key="+sessionKey);
       
   235 	baseString.append("ss=1");
       
   236 	baseString.append("uids="+uids);
       
   237 	baseString.append("v=1.0");
       
   238 	baseString.append(sessionSecret);
       
   239 
       
   240 	// Create the url
       
   241 	QUrl url("http://api.facebook.com/restserver.php?");
       
   242 	url.addQueryItem("api_key", apiKey);
       
   243 	url.addQueryItem("call_id", call_id);
       
   244 	url.addQueryItem("fields", "uid,name,pic,pic_square");
       
   245 #ifdef SMF_XMLPARSING
       
   246 	url.addQueryItem("format", "XML");
       
   247 #else
       
   248 	url.addQueryItem("format", "JSON");
       
   249 #endif
       
   250 	url.addQueryItem("method", "users.getInfo");
       
   251 	url.addQueryItem("session_key", sessionKey);
       
   252 	url.addQueryItem("ss", "1");
       
   253 	url.addQueryItem("uids", uids);
       
   254 	url.addQueryItem("v", "1.0");		
       
   255 	url.addQueryItem("sig", generateSignature(baseString));
       
   256 				
       
   257 	// Create the request, set the url
       
   258 	aRequest.iNetworkRequest.setUrl(url);
       
   259 	aRequest.iRequestType = SmfContactGetFriends;
       
   260 	aRequest.iPostData = NULL;
       
   261 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   262 	error = SmfPluginErrNone;
       
   263 
       
   264 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   265 	return error;
       
   266 
       
   267 	}
       
   268 
       
   269 
       
   270 /**
       
   271  * Method called by plugins to generate a signature string from a base string
       
   272  * @param aBaseString The base string
       
   273  * @return The md5 hash of the base string
       
   274  */
       
   275 QString FBContactFetcherPlugin::generateSignature(const QString aBaseString)
       
   276 	{
       
   277 	qDebug()<<"Inside FBContactFetcherPlugin::generateSignature()";
       
   278 	
       
   279 	// Create md5 hash of the signature string
       
   280     QByteArray byteArray;
       
   281     byteArray.insert(0, aBaseString.toAscii());
       
   282 
       
   283     QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex();
       
   284     QString returnString (md5Hash);
       
   285     return returnString;
       
   286 	}
       
   287 
       
   288 
       
   289 /**
       
   290  * Method to get the list of followers
       
   291  * @param aRequest [out] The request data to be sent to network
       
   292  * @param aPageNum The page to be extracted
       
   293  * @param aItemsPerPage Number of items per page
       
   294  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   295  */
       
   296 SmfPluginError FBContactFetcherPlugin::followers( SmfPluginRequestData &aRequest,
       
   297 		const int aPageNum , 
       
   298 		const int aItemsPerPage  )
       
   299 	{
       
   300 	Q_UNUSED(aRequest)
       
   301 	Q_UNUSED(aPageNum)
       
   302 	Q_UNUSED(aItemsPerPage)
       
   303 	qDebug()<<"Inside FBContactFetcherPlugin::followers()";
       
   304 	return SmfPluginErrServiceNotSupported; 
       
   305 	}
       
   306 
       
   307 
       
   308 /**
       
   309  * Method to search for a contact
       
   310  * @param aRequest [out] The request data to be sent to network
       
   311  * @param aContact contact to be searched
       
   312  * @param aPageNum The page to be extracted
       
   313  * @param aItemsPerPage Number of items per page
       
   314  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   315  */
       
   316 SmfPluginError FBContactFetcherPlugin::search( SmfPluginRequestData &aRequest,
       
   317 		const SmfContact &aContact,
       
   318 		const int aPageNum , 
       
   319 		const int aItemsPerPage  )
       
   320 	{
       
   321 	Q_UNUSED(aRequest)
       
   322 	Q_UNUSED(aContact)
       
   323 	Q_UNUSED(aPageNum)
       
   324 	Q_UNUSED(aItemsPerPage)
       
   325 	qDebug()<<"Inside FBContactFetcherPlugin::search()";
       
   326 	return SmfPluginErrServiceNotSupported; 
       
   327 	}
       
   328 
       
   329 
       
   330 /**
       
   331  * Method to search for contacts (friends) who are near the user.
       
   332  * Proximity defines accuracy level
       
   333  * @param aRequest [out] The request data to be sent to network
       
   334  * @param aLocation The location search criteria
       
   335  * @param aProximity location search boundary
       
   336  * @param aPageNum The page to be extracted
       
   337  * @param aItemsPerPage Number of items per page
       
   338  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   339  */
       
   340 SmfPluginError FBContactFetcherPlugin::searchNear( SmfPluginRequestData &aRequest,
       
   341 		const SmfLocation &aLocation,
       
   342 		const SmfLocationSearchBoundary &aProximity,
       
   343 		const int aPageNum , 
       
   344 		const int aItemsPerPage  )
       
   345 	{
       
   346 	Q_UNUSED(aRequest)
       
   347 	Q_UNUSED(aLocation)
       
   348 	Q_UNUSED(aProximity)
       
   349 	Q_UNUSED(aPageNum)
       
   350 	Q_UNUSED(aItemsPerPage)
       
   351 	qDebug()<<"Inside FBContactFetcherPlugin::searchNear()";
       
   352 	return SmfPluginErrServiceNotSupported; 
       
   353 	}
       
   354 
       
   355 
       
   356 /**
       
   357  * Method to get the list of groups
       
   358  * @param aRequest [out] The request data to be sent to network
       
   359  * @param aPageNum The page to be extracted
       
   360  * @param aItemsPerPage Number of items per page
       
   361  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   362  */
       
   363 SmfPluginError FBContactFetcherPlugin::groups( SmfPluginRequestData &aRequest,
       
   364 		const int aPageNum , 
       
   365 		const int aItemsPerPage  )
       
   366 	{
       
   367 	Q_UNUSED(aRequest)
       
   368 	Q_UNUSED(aPageNum)
       
   369 	Q_UNUSED(aItemsPerPage)
       
   370 	qDebug()<<"Inside FBContactFetcherPlugin::groups()";
       
   371 	return SmfPluginErrServiceNotSupported; 
       
   372 	}
       
   373 
       
   374 
       
   375 /**
       
   376  * Method to search for a contact in a group
       
   377  * @param aRequest [out] The request data to be sent to network
       
   378  * @param aGroup the group in which to search
       
   379  * @param aPageNum The page to be extracted
       
   380  * @param aItemsPerPage Number of items per page
       
   381  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   382  */
       
   383 SmfPluginError FBContactFetcherPlugin::searchInGroup( SmfPluginRequestData &aRequest,
       
   384 		const SmfGroup &aGroup,
       
   385 		const int aPageNum , 
       
   386 		const int aItemsPerPage  )
       
   387 	{
       
   388 	Q_UNUSED(aRequest)
       
   389 	Q_UNUSED(aGroup)
       
   390 	Q_UNUSED(aPageNum)
       
   391 	Q_UNUSED(aItemsPerPage)
       
   392 	qDebug()<<"Inside FBContactFetcherPlugin::searchInGroup()";
       
   393 	return SmfPluginErrServiceNotSupported; 
       
   394 	}
       
   395 
       
   396 
       
   397 /**
       
   398  * Customised method for SmfContactFetcherPlugin interface
       
   399  * @param aRequest [out] The request data to be sent to network
       
   400  * @param aOperation The operation type (should be known between 
       
   401  * the client interface and the plugin)
       
   402  * @param aData The data required to form the request (The type 
       
   403  * of data should be known between client and the plugin)
       
   404  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   405  */
       
   406 SmfPluginError FBContactFetcherPlugin::customRequest( SmfPluginRequestData &aRequest, 
       
   407 		const int &aOperation, QByteArray *aData )
       
   408 	{
       
   409 	Q_UNUSED(aRequest)
       
   410 	Q_UNUSED(aOperation)
       
   411 	Q_UNUSED(aData)
       
   412 	qDebug()<<"Inside FBContactFetcherPlugin::customRequest()";
       
   413 	return SmfPluginErrServiceNotSupported; 
       
   414 	}
       
   415 
       
   416 
       
   417 /**
       
   418  * The first method to be called in the plugin that implements this interface.
       
   419  * If this method is not called, plugin may not behave as expected.
       
   420  * Plugins are expected to save the aUtil handle and use and when required.
       
   421  * @param aUtil The instance of SmfPluginUtil
       
   422  */
       
   423 void FBContactFetcherPlugin::initialize( SmfPluginUtil *aUtil )
       
   424 	{
       
   425 	// Save the SmfPluginUtil handle
       
   426 	m_util = aUtil;
       
   427 	
       
   428 	// Create an instance of FBContactProviderBase
       
   429 	m_provider = new FBContactProviderBase;
       
   430 	m_provider->initialize();
       
   431 	}
       
   432 
       
   433 
       
   434 /**
       
   435  * Method to get the provider information
       
   436  * @return Instance of SmfProviderBase
       
   437  */
       
   438 SmfProviderBase* FBContactFetcherPlugin::getProviderInfo( )
       
   439 	{
       
   440 	return m_provider;
       
   441 	}
       
   442 
       
   443 
       
   444 /**
       
   445  * Method to get the result for a network request.
       
   446  * @param aOperation The type of operation to be requested
       
   447  * @param aTransportResult The result of transport operation
       
   448  * @param aResponse The QByteArray instance containing the network response.
       
   449  * The plugins should delete this instance once they have read the 
       
   450  * data from it.
       
   451  * @param aResult [out] An output parameter to the plugin manager.If the 
       
   452  * return value is SmfSendRequestAgain, QVariant will be of type 
       
   453  * SmfPluginRequestData.
       
   454  * For SmfGalleryPlugin: If last operation was pictures(), aResult will 
       
   455  * be of type QList<SmfPicture>. If last operation was description(), 
       
   456  * aResult will be of type QString. If last operation was upload() or 
       
   457  * postComment(), aResult will be of type bool.
       
   458  * @param aRetType [out] SmfPluginRetType
       
   459  * @param aPageResult [out] The SmfResultPage structure variable
       
   460  */
       
   461 SmfPluginError FBContactFetcherPlugin::responseAvailable( 
       
   462 		const SmfRequestTypeID aOperation,
       
   463 		const SmfTransportResult &aTransportResult, 
       
   464 		QByteArray *aResponse, 
       
   465 		QVariant* aResult, 
       
   466 		SmfPluginRetType &aRetType,
       
   467 		SmfResultPage &aPageResult )
       
   468 	{
       
   469 	Q_UNUSED(aPageResult)
       
   470 	qDebug()<<"Inside FBContactFetcherPlugin::responseAvailable()";
       
   471 	
       
   472 	SmfPluginError error = SmfPluginErrNetworkError;
       
   473 	
       
   474 	if( !aResponse || (0 == aResponse->size()) )
       
   475 		{
       
   476 		qDebug()<<"Response is NULL or empty";
       
   477 		aRetType = SmfRequestError;
       
   478 		return error;
       
   479 		}
       
   480 	
       
   481 	QByteArray response(*aResponse);
       
   482 	delete aResponse;
       
   483 	qDebug()<<"FB response = "<<QString(response);
       
   484 	qDebug()<<"FB response size = "<<response.size();
       
   485 	
       
   486 	
       
   487 	if(SmfTransportOpNoError == aTransportResult)
       
   488 		{
       
   489 		qDebug()<<"No transport error";
       
   490 		
       
   491 		if(SmfContactGetFriends == aOperation)
       
   492 			{
       
   493 			// For getting contact's UIDs
       
   494 			if(0 == chance)
       
   495 				{
       
   496 				qDebug()<<"For Getting friends UIDs";
       
   497 				chance = 1;
       
   498 				
       
   499 				QString errStr;
       
   500 				errStr.clear();
       
   501 				uids.clear();
       
   502 				
       
   503 #ifdef SMF_XMLPARSING
       
   504 				qDebug()<<"Xml parsing";
       
   505 				
       
   506 				// For getting contacts from xml response
       
   507 				QXmlStreamReader xml(response);
       
   508 				int count = 0;
       
   509 				while (!xml.atEnd())
       
   510 					{
       
   511 					xml.readNext();
       
   512 					if (xml.tokenType() == QXmlStreamReader::StartElement)
       
   513 						{
       
   514 						if (xml.name() == "uid")
       
   515 							{
       
   516 							qDebug()<<"uid tag found";
       
   517 							QString message(xml.readElementText());
       
   518 							uids.append(message);
       
   519 							uids.append(",");
       
   520 							count++;
       
   521 							if(5 == count)
       
   522 								break;
       
   523 							}
       
   524 						else if("error_msg" == xml.name())
       
   525 							{
       
   526 							qDebug()<<"error_msg tag found";
       
   527 							errStr.append(xml.readElementText());
       
   528 							break;
       
   529 							}
       
   530 						}
       
   531 					}
       
   532 				// Remove the final appended ',' for no error
       
   533 				if(0 == errStr.size())
       
   534 					uids.chop(1);
       
   535 #else
       
   536 				qDebug()<<"Json parsing";
       
   537 		
       
   538 				// For error in response
       
   539 				if(response.contains(QByteArray("error_msg")))
       
   540 					{
       
   541 					qDebug()<<"Response contains error, so parse and get the error code";
       
   542 					
       
   543 					bool ok;
       
   544 					QVariant result = m_util->parse(response, &ok);
       
   545 					if (!ok) 
       
   546 						{
       
   547 						qDebug()<<"An error occurred during json parsing, error = "<<m_util->errorString();
       
   548 						aRetType = SmfRequestError;
       
   549 						return SmfPluginErrParsingFailed;
       
   550 						}
       
   551 					else
       
   552 						{
       
   553 						QVariantMap map = result.toMap();
       
   554 						errStr.append(map["error_msg"].toString());
       
   555 						}
       
   556 					}
       
   557 				else
       
   558 					{
       
   559 					qDebug()<<"Response is ok, so don't parse";
       
   560 					response.remove(0, 1);
       
   561 					response.chop(1);
       
   562 					uids.append(response);
       
   563 					}
       
   564 			
       
   565 #endif
       
   566 				if(errStr.size())
       
   567 					{
       
   568 					qDebug()<<"Response error found = "<<errStr;
       
   569 					error = SmfPluginErrInvalidRequest;
       
   570 					aRetType = SmfRequestError;
       
   571 					aResult->setValue(errStr);
       
   572 					}
       
   573 				else
       
   574 					{
       
   575 					qDebug()<<"UIDs array = "<<uids;
       
   576 					error = SmfPluginErrNone;
       
   577 					aRetType = SmfSendRequestAgain;
       
   578 					}
       
   579 				}
       
   580 			
       
   581 			// For getting details of friends like name, pic etc.
       
   582 			else// if(1 == chance)
       
   583 				{
       
   584 				QList<SmfContact> list;
       
   585 			
       
   586 				QString errStr;
       
   587 				errStr.clear();
       
   588 				qDebug()<<"For Getting friends details";
       
   589 				chance = 0;
       
   590 				
       
   591 #ifdef SMF_XMLPARSING
       
   592 				qDebug()<<"Xml parsing";
       
   593 				
       
   594 				// For getting contacts from xml response
       
   595 				QXmlStreamReader xml(response);
       
   596 				while (!xml.atEnd())
       
   597 					{
       
   598 					xml.readNext();
       
   599 					
       
   600 					if (xml.tokenType() == QXmlStreamReader::StartElement)
       
   601 						{
       
   602 						if (xml.name() == "name")
       
   603 							{
       
   604 							QString message(xml.readElementText());
       
   605 							
       
   606 							QContactName contactname;
       
   607 							contactname.setFirstName(message);
       
   608 							contactname.setLastName(message);
       
   609 							QVariant nameVar = QVariant::fromValue(contactname);
       
   610 							contact.setValue("Name",nameVar);
       
   611 							}
       
   612 						else if (xml.name() == "uid")
       
   613 							{
       
   614 							QString message(xml.readElementText());
       
   615 							QContactGuid guid;
       
   616 							guid.setGuid(message);
       
   617 							QVariant guidVar = QVariant::fromValue(guid);
       
   618 							contact.setValue("Guid", guidVar);
       
   619 							}
       
   620 						else if (xml.name() == "pic_square")
       
   621 							{
       
   622 							QUrl url(xml.readElementText());
       
   623 							qDebug()<<"pic_square = "<<url.toString();
       
   624 							QContactAvatar avatar;
       
   625 							avatar.setImageUrl(url);
       
   626 							QVariant avatarVar = QVariant::fromValue(avatar);
       
   627 							contact.setValue("Avatar", avatarVar);
       
   628 							}
       
   629 						else if("error_msg" == xml.name())
       
   630 							{
       
   631 							qDebug()<<"error_msg tag found";
       
   632 							errStr.append(xml.readElementText());
       
   633 							break;
       
   634 							}
       
   635 						}
       
   636 					if (xml.tokenType() == QXmlStreamReader::EndElement)
       
   637 						{
       
   638 						if (xml.name() == "user")
       
   639 							{
       
   640 							qDebug()<<"avatar url = "<<contact.value("Avatar").value<QContactAvatar>().imageUrl().toString();
       
   641 							list.append(contact);
       
   642 							}
       
   643 						}
       
   644 					}
       
   645 #else
       
   646 				qDebug()<<"Json parsing";
       
   647 				
       
   648 				bool ok;
       
   649 				QVariant result = m_util->parse(response, &ok);
       
   650 				if (!ok) 
       
   651 					{
       
   652 					qDebug()<<"An error occurred during json parsing";
       
   653 					aRetType = SmfRequestError;
       
   654 					return SmfPluginErrParsingFailed;
       
   655 					}
       
   656 				
       
   657 				if(response.contains(QByteArray("error_msg")))
       
   658 					{
       
   659 					QVariantMap map = result.toMap();
       
   660 					errStr.append(map["error_msg"].toString());
       
   661 					}
       
   662 				else
       
   663 					{
       
   664 					QList<QVariant> list1 = result.toList();
       
   665 					QListIterator<QVariant> iter(list1);
       
   666 					while(iter.hasNext())
       
   667 						{
       
   668 						SmfContact contact;
       
   669 						QVariantMap map2 = iter.next().toMap();
       
   670 						qDebug()<<"name = "<<map2["name"].toString();
       
   671 						qDebug()<<"pic = "<<map2["pic"].toString();
       
   672 						qDebug()<<"uid = "<<map2["uid"].toString();
       
   673 						qDebug()<<"pic_square = "<<map2["pic_square"].toString();
       
   674 						
       
   675 						QContactName contactname;
       
   676 						contactname.setFirstName(map2["name"].toString());
       
   677 						contactname.setLastName(map2["name"].toString());
       
   678 						QVariant nameVar = QVariant::fromValue(contactname);
       
   679 						contact.setValue("Name",nameVar);
       
   680 						
       
   681 						QContactAvatar avatar;
       
   682 						QUrl url(map2["pic_square"].toString());
       
   683 						avatar.setImageUrl(url);
       
   684 						QVariant avatarVar = QVariant::fromValue(avatar);
       
   685 						contact.setValue("Avatar", avatarVar);
       
   686 						
       
   687 						QContactGuid guid;
       
   688 						guid.setGuid(map2["uid"].toString());
       
   689 						QVariant guidVar = QVariant::fromValue(guid);
       
   690 						contact.setValue("Guid", guidVar);
       
   691 						
       
   692 						
       
   693 #if 0 // tesing SmfContact serialization
       
   694 						QContactAddress address;
       
   695 						address.setCountry("sampleCountry");
       
   696 						address.setLocality("samplelocatlity");
       
   697 						address.setPostOfficeBox("12345");
       
   698 						address.setPostcode("67890");
       
   699 						address.setRegion("sampleregion");
       
   700 						address.setStreet("sampleStreet");
       
   701 						QStringList list1;
       
   702 						list1.append("type1");
       
   703 						address.setSubTypes(list1);
       
   704 						QVariant var1 = QVariant::fromValue(address);
       
   705 						contact.setValue("Address", var1);
       
   706 								
       
   707 						QContactAnniversary anni;
       
   708 						anni.setCalendarId("12345");
       
   709 						anni.setEvent("6547645");
       
   710 						QDate date1(2010, 06, 18);
       
   711 						anni.setOriginalDate(date1);
       
   712 						anni.setSubType("76345764");
       
   713 						QVariant var2 = QVariant::fromValue(anni);
       
   714 						contact.setValue("Anniversary", var2);
       
   715 						
       
   716 						QContactAvatar avatar;
       
   717 						QUrl url(map2["pic_square"].toString());
       
   718 						avatar.setImageUrl(url);
       
   719 						avatar.setVideoUrl(url);
       
   720 						QVariant avatarVar = QVariant::fromValue(avatar);
       
   721 						contact.setValue("Avatar", avatarVar);
       
   722 						
       
   723 						QContactBirthday bday;
       
   724 						date1.setDate(1983,9,12);
       
   725 						bday.setDate(date1);
       
   726 						QVariant var3 = QVariant::fromValue(bday);
       
   727 						contact.setValue("Birthday", var3);
       
   728 						
       
   729 						QContactId id;
       
   730 						QContactLocalId localid = 10;
       
   731 						id.setLocalId(localid);
       
   732 						id.setManagerUri("djfhjhyd");
       
   733 						QVariant v4 = QVariant::fromValue(id);
       
   734 						contact.setValue("ContactId", v4);
       
   735 						
       
   736 						QContactEmailAddress email;
       
   737 						email.setEmailAddress("sdjfhujsdhf@kjdfk.com");
       
   738 						QVariant v5 = QVariant::fromValue(email);
       
   739 						contact.setValue("EmailAddress", v5);
       
   740 						
       
   741 						QContactGender gender;
       
   742 						gender.setGender("female");
       
   743 						QVariant v6 = QVariant::fromValue(gender);
       
   744 						contact.setValue("Gender", v6);
       
   745 						
       
   746 						QTime t(14,0,0);
       
   747 						QDateTime time1(date1, t);
       
   748 						QContactGeoLocation geo;
       
   749 						geo.setAccuracy(1.23);
       
   750 						geo.setAltitude(2.34);
       
   751 						geo.setAltitudeAccuracy(3.45);
       
   752 						geo.setHeading(4.56);
       
   753 						geo.setLabel("hdgfhdgf");
       
   754 						geo.setLatitude(6.78);
       
   755 						geo.setLongitude(7.89);
       
   756 						geo.setSpeed(23.456);
       
   757 						geo.setTimestamp(time1);
       
   758 						QVariant v7 = QVariant::fromValue(geo);
       
   759 						contact.setValue("Geolocation", v7);
       
   760 						
       
   761 						QContactGuid guid;
       
   762 						guid.setGuid(map2["uid"].toString());
       
   763 						QVariant guidVar = QVariant::fromValue(guid);
       
   764 						contact.setValue("Guid", guidVar);
       
   765 						
       
   766 						QContactName contactname;
       
   767 						contactname.setFirstName(map2["name"].toString());
       
   768 						contactname.setLastName(map2["name"].toString());
       
   769 						contactname.setCustomLabel("jsdhfjhsdf");
       
   770 						contactname.setMiddleName("kjtiuer");
       
   771 						contactname.setPrefix("djfhj");
       
   772 						contactname.setSuffix("jdhf");
       
   773 						QVariant nameVar = QVariant::fromValue(contactname);
       
   774 						contact.setValue("Name",nameVar);
       
   775 						
       
   776 						QContactNickname nickname;
       
   777 						nickname.setNickname("ddfffff");
       
   778 						QVariant v8 = QVariant::fromValue(nickname);
       
   779 						contact.setValue("Nickname", v8);
       
   780 						
       
   781 						QContactNote note;
       
   782 						note.setNote("jdhfjsdhf");
       
   783 						QVariant v9 = QVariant::fromValue(note);
       
   784 						contact.setValue("Note", v9);
       
   785 						
       
   786 						QContactOnlineAccount account;
       
   787 						account.setAccountUri("jnjhd");
       
   788 						account.setCapabilities(list1);
       
   789 						account.setServiceProvider("jhjyhjk");
       
   790 						list1.append("345");
       
   791 						account.setSubTypes(list1);
       
   792 						QVariant v10 = QVariant::fromValue(account);
       
   793 						contact.setValue("OnlineAccount", v10);
       
   794 						
       
   795 						QContactOrganization org;
       
   796 						org.setAssistantName("mnjhj");
       
   797 						org.setDepartment(list1);
       
   798 						org.setLocation("mnjh");
       
   799 						org.setLogoUrl(url);
       
   800 						org.setName("kkiujki");
       
   801 						org.setRole("nhfg");
       
   802 						org.setTitle("mnhfg");
       
   803 						QVariant v11 = QVariant::fromValue(org);
       
   804 						contact.setValue("Organization", v11);
       
   805 						
       
   806 						QContactPhoneNumber number;
       
   807 						number.setNumber("76347");
       
   808 						list1.append("jhsdjhsdf");
       
   809 						number.setSubTypes(list1);
       
   810 						QVariant v12 = QVariant::fromValue(number);
       
   811 						contact.setValue("PhoneNumber", v12);
       
   812 						
       
   813 						QContactPresence presence;
       
   814 						presence.setCustomMessage("djhfjhsd");
       
   815 						presence.setNickname("kajsiju");
       
   816 						QContactPresence::PresenceState state = QContactPresence::PresenceBusy;
       
   817 						presence.setPresenceState(state);
       
   818 						presence.setPresenceStateImageUrl(url);
       
   819 						presence.setPresenceStateText("Busy Now");
       
   820 						presence.setTimestamp(time1);
       
   821 						QVariant v13 = QVariant::fromValue(presence);
       
   822 						contact.setValue("Presence", v13);
       
   823 						
       
   824 						QContactThumbnail thumbnail;
       
   825 						QImage image("c://Data//TestUploadPics//BlueHills.jpg");
       
   826 						qDebug()<<"Image bytecount = "<<image.byteCount();
       
   827 						thumbnail.setThumbnail(image);
       
   828 						QVariant v14 = QVariant::fromValue(thumbnail);
       
   829 						contact.setValue("Thumbnail", v14);
       
   830 						
       
   831 						QContactTimestamp timestamp;
       
   832 						timestamp.setCreated(time1);
       
   833 						timestamp.setLastModified(time1);
       
   834 						QVariant v15 = QVariant::fromValue(timestamp);
       
   835 						contact.setValue("Timestamp", v15);
       
   836 						
       
   837 						QContactType type;
       
   838 						type.setType("default");
       
   839 						QVariant v16 = QVariant::fromValue(type);
       
   840 						contact.setValue("Type", v16);
       
   841 						
       
   842 						QContactUrl myurl;
       
   843 						myurl.setUrl("My Url");
       
   844 						myurl.setSubType("my url type");
       
   845 						QVariant v17 = QVariant::fromValue(myurl);
       
   846 						contact.setValue("Url", v17);
       
   847 
       
   848 #endif
       
   849 						
       
   850 						list.append(contact);
       
   851 						}
       
   852 					}
       
   853 #endif
       
   854 				
       
   855 				if(errStr.size())
       
   856 					{
       
   857 					qDebug()<<"Response error found = "<<errStr;
       
   858 					error = SmfPluginErrInvalidRequest;
       
   859 					aRetType = SmfRequestError;
       
   860 					aResult->setValue(errStr);
       
   861 					}
       
   862 				else
       
   863 					{
       
   864 					qDebug()<<"list count = "<<list.count();
       
   865 					aResult->setValue(list);
       
   866 					aRetType = SmfRequestComplete;
       
   867 					error = SmfPluginErrNone;
       
   868 					}
       
   869 				}
       
   870 			}
       
   871 		else
       
   872 			{
       
   873 			qDebug()<<"Service unsupported, as of now only SmfContactGetFriends!!!";
       
   874 			aRetType = SmfRequestError;
       
   875 			error = SmfPluginErrServiceNotSupported;
       
   876 			}
       
   877 		}
       
   878 
       
   879 	else if(SmfTransportOpOperationCanceledError == aTransportResult)
       
   880 		{
       
   881 		qDebug()<<"Operation Cancelled !!!";
       
   882 		error = SmfPluginErrCancelComplete;
       
   883 		aRetType = SmfRequestComplete;
       
   884 		}
       
   885 
       
   886 	else
       
   887 		{
       
   888 		qDebug()<<"Transport Error !!!";
       
   889 		error = SmfPluginErrNetworkError;
       
   890 		aRetType = SmfRequestError;
       
   891 		}
       
   892 	
       
   893 	return error;
       
   894 	}
       
   895 
       
   896 
       
   897 /**
       
   898  * Destructor
       
   899  */
       
   900 FBContactProviderBase::~FBContactProviderBase( )
       
   901 	{
       
   902 	}
       
   903 
       
   904 
       
   905 /**
       
   906  * Method to get the Localisable name of the service.
       
   907  * @return The Localisable name of the service.
       
   908  */
       
   909 QString FBContactProviderBase::serviceName( ) const
       
   910 	{
       
   911 	return m_serviceName;
       
   912 	}
       
   913 
       
   914 
       
   915 /**
       
   916  * Method to get the Logo of the service
       
   917  * @return The Logo of the service
       
   918  */
       
   919 QImage FBContactProviderBase::serviceIcon( ) const
       
   920 	{
       
   921 	return m_serviceIcon;
       
   922 	}
       
   923 
       
   924 
       
   925 /**
       
   926  * Method to get the Readable service description
       
   927  * @return The Readable service description
       
   928  */
       
   929 QString FBContactProviderBase::description( ) const
       
   930 	{
       
   931 	return m_description;
       
   932 	}
       
   933 
       
   934 
       
   935 /**
       
   936  * Method to get the Website of the service
       
   937  * @return The Website of the service
       
   938  */
       
   939 QUrl FBContactProviderBase::serviceUrl( ) const
       
   940 	{
       
   941 	return m_serviceUrl;
       
   942 	}
       
   943 
       
   944 
       
   945 /**
       
   946  * Method to get the URL of the Application providing this service
       
   947  * @return The URL of the Application providing this service
       
   948  */
       
   949 QUrl FBContactProviderBase::applicationUrl( ) const
       
   950 	{
       
   951 	return m_applicationUrl;
       
   952 	}
       
   953 
       
   954 
       
   955 /**
       
   956  * Method to get the Icon of the application
       
   957  * @return The Icon of the application
       
   958  */
       
   959 QImage FBContactProviderBase::applicationIcon( ) const
       
   960 	{
       
   961 	return m_applicationIcon;
       
   962 	}
       
   963 
       
   964 /**
       
   965 * Method to get the list of interfaces that this provider support
       
   966 * @return List of supported Interafces
       
   967 */
       
   968 QList<QString> FBContactProviderBase::supportedInterfaces( ) const
       
   969 	{
       
   970 	return m_supportedInterfaces;
       
   971 	}
       
   972 
       
   973 /**
       
   974 * Method to get the list of languages supported by this service provider
       
   975 * @return a QStringList of languages supported by this service 
       
   976 * provider in 2 letter ISO 639-1 format.
       
   977 */
       
   978 QStringList FBContactProviderBase::supportedLanguages( ) const
       
   979 	{
       
   980 	return m_supportedLangs;
       
   981 	}
       
   982 
       
   983 /**
       
   984  * Method to get the Plugin specific ID
       
   985  * @return The Plugin specific ID
       
   986  */
       
   987 QString FBContactProviderBase::pluginId( ) const
       
   988 	{
       
   989 	return m_pluginId;
       
   990 	}
       
   991 
       
   992 
       
   993 /**
       
   994  * Method to get the ID of the authentication application 
       
   995  * for this service
       
   996  * @param aProgram The authentication application name
       
   997  * @param aArguments List of arguments required for authentication app
       
   998  * @param aMode Strting mode for authentication application
       
   999  * @return The ID of the authentication application 
       
  1000  */
       
  1001 QString FBContactProviderBase::authenticationApp( QString &aProgram, 
       
  1002 		QStringList & aArguments, 
       
  1003 		QIODevice::OpenModeFlag aMode ) const
       
  1004 	{
       
  1005 	Q_UNUSED(aProgram)
       
  1006 	Q_UNUSED(aArguments)
       
  1007 	Q_UNUSED(aMode)
       
  1008 	return m_authAppId;
       
  1009 	}
       
  1010 
       
  1011 
       
  1012 /**
       
  1013  * Method to get the unique registration ID provided by the 
       
  1014  * Smf for authorised plugins
       
  1015  * @return The unique registration ID/token provided by the Smf for 
       
  1016  * authorised plugins
       
  1017  */
       
  1018 QString FBContactProviderBase::smfRegistrationId( ) const
       
  1019 	{
       
  1020 	return m_smfRegToken;
       
  1021 	}
       
  1022 
       
  1023 
       
  1024 /**
       
  1025  * Method that initializes this class. This method should be called 
       
  1026  * from the initialize() method of the FBContactFetcherPlugin class
       
  1027  */
       
  1028 void FBContactProviderBase::initialize()
       
  1029 	{
       
  1030 	m_serviceName = "Facebook";
       
  1031 	m_description = "Facebook contact fetcher plugin description";
       
  1032 	m_serviceUrl = QUrl(QString("http://api.facebook.com"));
       
  1033 	m_pluginId = "fbcontactfetcherplugin.qtplugin";
       
  1034 	m_authAppId = "Facebook AuthAppId";
       
  1035 	m_smfRegToken = "Facebook RegToken";
       
  1036 	m_supportedInterfaces.append("org.symbian.smf.plugin.contact.fetcher/v0.2");
       
  1037 	}
       
  1038 
       
  1039 
       
  1040 /*
       
  1041  * Export Macro
       
  1042  * plugin name : fbcontactfetcherplugin
       
  1043  * plugin class : FBContactFetcherPlugin
       
  1044  */
       
  1045 Q_EXPORT_PLUGIN2( fbcontactfetcherplugin, FBContactFetcherPlugin )
       
  1046