example/fbcontactfetcherplugin/fbcontactfetcherplugin.cpp
changeset 17 106a4bfcb866
parent 14 a469c0e6e7fb
child 23 574948b60dab
equal deleted inserted replaced
16:b78fa4cdbf2b 17:106a4bfcb866
    23 #include <QCryptographicHash>
    23 #include <QCryptographicHash>
    24 #include <QTextStream>
    24 #include <QTextStream>
    25 #include <QFile>
    25 #include <QFile>
    26 #include <QMap>
    26 #include <QMap>
    27 #include <QListIterator>
    27 #include <QListIterator>
       
    28 #include <QSettings>
       
    29 #include <smfpluginutil.h>
    28 #ifdef SMF_XMLPARSING
    30 #ifdef SMF_XMLPARSING
    29 #include <QXmlStreamReader>
    31 #include <QXmlStreamReader>
    30 #endif
    32 #endif
    31 
    33 
    32 #include "fbcontactfetcherplugin.h"
    34 #include "fbcontactfetcherplugin.h"
    37 
    39 
    38 #ifdef SMF_XMLPARSING
    40 #ifdef SMF_XMLPARSING
    39 SmfContact contact;
    41 SmfContact contact;
    40 #endif
    42 #endif
    41 
    43 
       
    44 // Todo:- Macro added for limiting items fetched to recent 5
       
    45 // Remove after demo
       
    46 #define SETLIMITOFFIVEFORSMFDEMO 1
    42 
    47 
    43 /**
    48 /**
    44  * Destructor
    49  * Destructor
    45  */
    50  */
    46 FBContactFetcherPlugin::~FBContactFetcherPlugin( )
    51 FBContactFetcherPlugin::~FBContactFetcherPlugin( )
    47 	{
    52 	{
    48 	if(m_provider)
    53 	if(m_provider)
    49 		delete m_provider;
    54 		delete m_provider;
    50 	}
    55 	}
       
    56 
       
    57 /**
       
    58  * Method to interpret the key sets obtained from credential manager 
       
    59  * @param aApiKey [out] The api key
       
    60  * @param aApiSecret [out] The api secret
       
    61  * @param aSessionKey [out] The session key
       
    62  * @param aSessionSecret [out] The session secret
       
    63  * @param aAppId [out] The application ID
       
    64  */
       
    65 void FBContactFetcherPlugin::fetchKeys( QString &aApiKey, 
       
    66 		QString &aApiSecret, 
       
    67 		QString &aSessionKey, 
       
    68 		QString &aSessionSecret )
       
    69 	{
       
    70 	qDebug()<<"Inside FBContactFetcherPlugin::fetchKeys()";
       
    71 
       
    72 	qDebug()<<"Reg Token = "<<m_provider->m_smfRegToken;
       
    73 	qDebug()<<"Expiry Date as int = "<<m_provider->m_validity.toTime_t();
       
    74 	
       
    75 	SmfAuthParams keys;
       
    76 	SmfPluginUtil util;
       
    77 	util.getAuthKeys(keys, m_provider->m_smfRegToken, 
       
    78 			m_provider->m_validity, m_provider->m_pluginId);
       
    79 	
       
    80 	qDebug()<<"Number of key-value pairs = "<<keys.count();
       
    81 	
       
    82     QByteArray keyName;
       
    83     keyName.append("ApiKey");
       
    84 	aApiKey.append(keys.value(keyName));
       
    85 	
       
    86     keyName.clear();
       
    87     keyName.append("ApiSecret");
       
    88 	aApiSecret.append(keys.value(keyName));
       
    89 	
       
    90 	keyName.clear();
       
    91     keyName.append("SessionKey");
       
    92 	aSessionKey.append(keys.value(keyName));
       
    93 	
       
    94 	keyName.clear();
       
    95     keyName.append("SessionSecret");
       
    96 	aSessionSecret.append(keys.value(keyName));
       
    97 	
       
    98 	qDebug()<<"Api Key = "<<aApiKey;
       
    99 	qDebug()<<"Api Secret = "<<aApiSecret;
       
   100 	qDebug()<<"session Key = "<<aSessionKey;
       
   101 	qDebug()<<"session Secret = "<<aSessionSecret;
       
   102 	}
       
   103 
    51 
   104 
    52 /**
   105 /**
    53  * Method to get the list of friends
   106  * Method to get the list of friends
    54  * @param aRequest [out] The request data to be sent to network
   107  * @param aRequest [out] The request data to be sent to network
    55  * @param aPageNum The page to be extracted
   108  * @param aPageNum The page to be extracted
    64 	
   117 	
    65 	if(0 == chance)
   118 	if(0 == chance)
    66 		return getFriendsUids(aRequest, aPageNum, aItemsPerPage);
   119 		return getFriendsUids(aRequest, aPageNum, aItemsPerPage);
    67 	else// if(1 == chance)
   120 	else// if(1 == chance)
    68 		return getFriendsDetails(aRequest, aPageNum, aItemsPerPage);
   121 		return getFriendsDetails(aRequest, aPageNum, aItemsPerPage);
    69 
       
    70 	}
   122 	}
    71 
   123 
    72 /**
   124 /**
    73  * Method to get Facebook specific UIDs of the user's friends
   125  * Method to get Facebook specific UIDs of the user's friends
    74  * @param aRequest [out] The request data to be sent to network
   126  * @param aRequest [out] The request data to be sent to network
    91 		return error;
   143 		return error;
    92 		}
   144 		}
    93 	
   145 	
    94 	qDebug()<<"Valid arguments";
   146 	qDebug()<<"Valid arguments";
    95 
   147 
    96 #if 1
   148 	// Get the key sets from SMF Plugin Utility class.
    97 // Reading the keys, CSM Stubbed - START
   149 	QString apiKey;
    98 	QFile file("c:\\data\\FacebookKeys.txt");
   150 	QString apiSecret;
    99 	if (!file.open(QIODevice::ReadOnly))
   151 	QString sessionKey;
   100 		{
   152 	QString sessionSecret;
   101 		qDebug()<<"File to read the keys could not be opened";
   153 	fetchKeys(apiKey, apiSecret, sessionKey, sessionSecret);
   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 		
   154 		
   123 	// Get the current date and time and convert it to seconds as a string
   155 	// 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);
   156 	QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10);
   125 	
   157 	
   126 	// Create the API signature string
   158 	// Create the API signature string
   188 	Q_UNUSED(aItemsPerPage)
   220 	Q_UNUSED(aItemsPerPage)
   189 	qDebug()<<"Inside FBContactFetcherPlugin::getFriendsDetails()";
   221 	qDebug()<<"Inside FBContactFetcherPlugin::getFriendsDetails()";
   190 	
   222 	
   191 	SmfPluginError error = SmfPluginErrUserNotLoggedIn;
   223 	SmfPluginError error = SmfPluginErrUserNotLoggedIn;
   192 	
   224 	
   193 #if 1
   225 	// Get the key sets from SMF Plugin Utility class.
   194 // Reading the keys, CSM Stubbed - START
   226 	QString apiKey;
   195 	QFile file("c:\\data\\FacebookKeys.txt");
   227 	QString apiSecret;
   196 	if (!file.open(QIODevice::ReadOnly))
   228 	QString sessionKey;
   197 		{
   229 	QString sessionSecret;
   198 		qDebug()<<"File to read the keys could not be opened";
   230 	fetchKeys(apiKey, apiSecret, sessionKey, sessionSecret);
   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 		
   231 		
   220 	// Get the current date and time and convert it to sec as a string
   232 	// 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);
   233 	QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10);
   222 	
   234 	
   223 	// Create the API signature string
   235 	// Create the API signature string
   224 	QString baseString;
   236 	QString baseString;
   225 	baseString.append("api_key="+apiKey);
   237 	baseString.append("api_key="+apiKey);
   226 	baseString.append("call_id="+call_id);
   238 	baseString.append("call_id="+call_id);
   227 	baseString.append("fields=uid,name,pic,pic_square");
   239 	baseString.append("fields=uid,name,pic,pic_square,status");
   228 #ifdef SMF_XMLPARSING
   240 #ifdef SMF_XMLPARSING
   229 	baseString.append("format=XML");
   241 	baseString.append("format=XML");
   230 #else
   242 #else
   231 	baseString.append("format=JSON");
   243 	baseString.append("format=JSON");
   232 #endif
   244 #endif
   233 	baseString.append("method=users.getInfo");
   245 	baseString.append("method=users.getInfo");
   234 	baseString.append("session_key="+sessionKey);
   246 	baseString.append("session_key="+sessionKey);
   235 	baseString.append("ss=1");
   247 	baseString.append("ss=1");
       
   248 	
       
   249 #ifdef SETLIMITOFFIVEFORSMFDEMO // limiting to fetch only first 5 contact details
       
   250 	
       
   251 	QStringList firstFiveUids = uids.split(',');
       
   252 	uids.clear();
       
   253 	int i = 0;
       
   254 	while(i < 5)
       
   255 		{
       
   256 		uids.append(firstFiveUids.at(i));
       
   257 		uids.append(",");
       
   258 		i++;
       
   259 		}
       
   260 	if(firstFiveUids.count()>5)
       
   261 		uids.chop(1);
       
   262 	qDebug()<<"Limited to 5 uid string = "<<uids;
       
   263 #endif
       
   264 	
   236 	baseString.append("uids="+uids);
   265 	baseString.append("uids="+uids);
   237 	baseString.append("v=1.0");
   266 	baseString.append("v=1.0");
   238 	baseString.append(sessionSecret);
   267 	baseString.append(sessionSecret);
   239 
   268 
   240 	// Create the url
   269 	// Create the url
   241 	QUrl url("http://api.facebook.com/restserver.php?");
   270 	QUrl url("http://api.facebook.com/restserver.php?");
   242 	url.addQueryItem("api_key", apiKey);
   271 	url.addQueryItem("api_key", apiKey);
   243 	url.addQueryItem("call_id", call_id);
   272 	url.addQueryItem("call_id", call_id);
   244 	url.addQueryItem("fields", "uid,name,pic,pic_square");
   273 	url.addQueryItem("fields", "uid,name,pic,pic_square,status");
   245 #ifdef SMF_XMLPARSING
   274 #ifdef SMF_XMLPARSING
   246 	url.addQueryItem("format", "XML");
   275 	url.addQueryItem("format", "XML");
   247 #else
   276 #else
   248 	url.addQueryItem("format", "JSON");
   277 	url.addQueryItem("format", "JSON");
   249 #endif
   278 #endif
   415 
   444 
   416 
   445 
   417 /**
   446 /**
   418  * The first method to be called in the plugin that implements this interface.
   447  * 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.
   448  * 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.
   449  */
   421  * @param aUtil The instance of SmfPluginUtil
   450 void FBContactFetcherPlugin::initialize( )
   422  */
   451 	{
   423 void FBContactFetcherPlugin::initialize( SmfPluginUtil *aUtil )
       
   424 	{
       
   425 	// Save the SmfPluginUtil handle
       
   426 	m_util = aUtil;
       
   427 	
       
   428 	// Create an instance of FBContactProviderBase
   452 	// Create an instance of FBContactProviderBase
   429 	m_provider = new FBContactProviderBase;
   453 	m_provider = new FBContactProviderBase;
   430 	m_provider->initialize();
   454 	m_provider->initialize();
   431 	}
   455 	}
   432 
   456 
   478 		return error;
   502 		return error;
   479 		}
   503 		}
   480 	
   504 	
   481 	QByteArray response(*aResponse);
   505 	QByteArray response(*aResponse);
   482 	delete aResponse;
   506 	delete aResponse;
   483 	qDebug()<<"FB response = "<<QString(response);
   507 
   484 	qDebug()<<"FB response size = "<<response.size();
   508 	QFile respFile("c://data//SmfPluginFBContactResponse.txt");
       
   509 	if(!respFile.open(QIODevice::WriteOnly))
       
   510 		{
       
   511 		qDebug()<<"File to write the response could not be opened, so writing to this file";
       
   512 		qDebug()<<"Flickr response = "<<QString(response);
       
   513 		}
       
   514 	else
       
   515 		{
       
   516 		respFile.write(response);
       
   517 		respFile.close();
       
   518 		qDebug()<<"Writing FB response to a file named 'SmfPluginFBContactResponse.txt'";
       
   519 		}
       
   520 	
       
   521 	qDebug()<<"Response size = "<<response.size();
   485 	
   522 	
   486 	
   523 	
   487 	if(SmfTransportOpNoError == aTransportResult)
   524 	if(SmfTransportOpNoError == aTransportResult)
   488 		{
   525 		{
   489 		qDebug()<<"No transport error";
   526 		qDebug()<<"No transport error";
   516 							qDebug()<<"uid tag found";
   553 							qDebug()<<"uid tag found";
   517 							QString message(xml.readElementText());
   554 							QString message(xml.readElementText());
   518 							uids.append(message);
   555 							uids.append(message);
   519 							uids.append(",");
   556 							uids.append(",");
   520 							count++;
   557 							count++;
       
   558 #ifdef SETLIMITOFFIVEFORSMFDEMO
   521 							if(5 == count)
   559 							if(5 == count)
   522 								break;
   560 								break;
       
   561 #endif
   523 							}
   562 							}
   524 						else if("error_msg" == xml.name())
   563 						else if("error_msg" == xml.name())
   525 							{
   564 							{
   526 							qDebug()<<"error_msg tag found";
   565 							qDebug()<<"error_msg tag found";
   527 							errStr.append(xml.readElementText());
   566 							errStr.append(xml.readElementText());
   539 				if(response.contains(QByteArray("error_msg")))
   578 				if(response.contains(QByteArray("error_msg")))
   540 					{
   579 					{
   541 					qDebug()<<"Response contains error, so parse and get the error code";
   580 					qDebug()<<"Response contains error, so parse and get the error code";
   542 					
   581 					
   543 					bool ok;
   582 					bool ok;
   544 					QVariant result = m_util->parse(response, &ok);
   583 					SmfPluginUtil util;
       
   584 					QVariant result = util.parse(response, &ok);
   545 					if (!ok) 
   585 					if (!ok) 
   546 						{
   586 						{
   547 						qDebug()<<"An error occurred during json parsing, error = "<<m_util->errorString();
   587 						qDebug()<<"An error occurred during json parsing, error = "<<util.errorString();
   548 						aRetType = SmfRequestError;
   588 						aRetType = SmfRequestError;
   549 						return SmfPluginErrParsingFailed;
   589 						return SmfPluginErrParsingFailed;
   550 						}
   590 						}
   551 					else
   591 					else
   552 						{
   592 						{
   644 					}
   684 					}
   645 #else
   685 #else
   646 				qDebug()<<"Json parsing";
   686 				qDebug()<<"Json parsing";
   647 				
   687 				
   648 				bool ok;
   688 				bool ok;
   649 				QVariant result = m_util->parse(response, &ok);
   689 				SmfPluginUtil util;
       
   690 				QVariant result = util.parse(response, &ok);
   650 				if (!ok) 
   691 				if (!ok) 
   651 					{
   692 					{
   652 					qDebug()<<"An error occurred during json parsing";
   693 					qDebug()<<"An error occurred during json parsing";
   653 					aRetType = SmfRequestError;
   694 					aRetType = SmfRequestError;
   654 					return SmfPluginErrParsingFailed;
   695 					return SmfPluginErrParsingFailed;
   677 						contactname.setLastName(map2["name"].toString());
   718 						contactname.setLastName(map2["name"].toString());
   678 						QVariant nameVar = QVariant::fromValue(contactname);
   719 						QVariant nameVar = QVariant::fromValue(contactname);
   679 						contact.setValue("Name",nameVar);
   720 						contact.setValue("Name",nameVar);
   680 						
   721 						
   681 						QContactAvatar avatar;
   722 						QContactAvatar avatar;
   682 						QUrl url(map2["pic_square"].toString());
   723 						QUrl url;
       
   724 						if(0 != map2["pic_square"].toString().size())
       
   725 							url = map2["pic_square"].toString();
       
   726 						else
       
   727 							url = QString("http://static.ak.fbcdn.net/rsrc.php/z5HB7/hash/ecyu2wwn.gif");
   683 						avatar.setImageUrl(url);
   728 						avatar.setImageUrl(url);
   684 						QVariant avatarVar = QVariant::fromValue(avatar);
   729 						QVariant avatarVar = QVariant::fromValue(avatar);
   685 						contact.setValue("Avatar", avatarVar);
   730 						contact.setValue("Avatar", avatarVar);
   686 						
   731 						
   687 						QContactGuid guid;
   732 						QContactGuid guid;
   688 						guid.setGuid(map2["uid"].toString());
   733 						guid.setGuid(map2["uid"].toString());
   689 						QVariant guidVar = QVariant::fromValue(guid);
   734 						QVariant guidVar = QVariant::fromValue(guid);
   690 						contact.setValue("Guid", guidVar);
   735 						contact.setValue("Guid", guidVar);
       
   736 						
       
   737 						QContactPresence presence;
       
   738 						presence.setCustomMessage(map2["status"].toString());
       
   739 						QVariant presVar = QVariant::fromValue(presence);
       
   740 						contact.setValue("Presence", presVar);
   691 						
   741 						
   692 						
   742 						
   693 #if 0 // tesing SmfContact serialization
   743 #if 0 // tesing SmfContact serialization
   694 						QContactAddress address;
   744 						QContactAddress address;
   695 						address.setCountry("sampleCountry");
   745 						address.setCountry("sampleCountry");
  1029 	{
  1079 	{
  1030 	m_serviceName = "Facebook";
  1080 	m_serviceName = "Facebook";
  1031 	m_description = "Facebook contact fetcher plugin description";
  1081 	m_description = "Facebook contact fetcher plugin description";
  1032 	m_serviceUrl = QUrl(QString("http://api.facebook.com"));
  1082 	m_serviceUrl = QUrl(QString("http://api.facebook.com"));
  1033 	m_pluginId = "fbcontactfetcherplugin.qtplugin";
  1083 	m_pluginId = "fbcontactfetcherplugin.qtplugin";
  1034 	m_authAppId = "Facebook AuthAppId";
  1084 	m_authAppId = "0xEFE2FD23";
  1035 	m_smfRegToken = "Facebook RegToken";
       
  1036 	m_supportedInterfaces.append("org.symbian.smf.plugin.contact.fetcher/v0.2");
  1085 	m_supportedInterfaces.append("org.symbian.smf.plugin.contact.fetcher/v0.2");
       
  1086 	QSettings iSettings;
       
  1087 	m_smfRegToken = iSettings.value("FBCMRegToken").toString();
       
  1088 	m_validity = iSettings.value("FBExpiryTime").toDateTime();
  1037 	}
  1089 	}
  1038 
  1090 
  1039 
  1091 
  1040 /*
  1092 /*
  1041  * Export Macro
  1093  * Export Macro