example/flickrcontactfetcherplugin/flickrcontactfetcherplugin.cpp
changeset 23 574948b60dab
parent 17 106a4bfcb866
child 26 83d6a149c755
equal deleted inserted replaced
22:b2eb79881f9d 23:574948b60dab
   181  */
   181  */
   182 SmfPluginError FlickrContactFetcherPlugin::followers( SmfPluginRequestData &aRequest,
   182 SmfPluginError FlickrContactFetcherPlugin::followers( SmfPluginRequestData &aRequest,
   183 		const int aPageNum , 
   183 		const int aPageNum , 
   184 		const int aItemsPerPage  )
   184 		const int aItemsPerPage  )
   185 	{
   185 	{
       
   186 #ifndef TESTINGTHISFUNCTION
   186 	Q_UNUSED(aRequest)
   187 	Q_UNUSED(aRequest)
   187 	Q_UNUSED(aPageNum)
   188 	Q_UNUSED(aPageNum)
   188 	Q_UNUSED(aItemsPerPage)
   189 	Q_UNUSED(aItemsPerPage)
   189 	qDebug()<<"Inside FlickrContactFetcherPlugin::followers()";
   190 	qDebug()<<"Inside FlickrContactFetcherPlugin::followers()";
   190 	return SmfPluginErrServiceNotSupported; 
   191 	return SmfPluginErrServiceNotSupported; 
       
   192 #else
       
   193 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   194 
       
   195 	// invalid arguments
       
   196 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
   197 		{
       
   198 		qDebug()<<"Invalid arguments";
       
   199 		return error;
       
   200 		}
       
   201 	
       
   202 	qDebug()<<"Valid arguments";
       
   203 	
       
   204 	// Get the key sets from SMF Plugin Utility class.
       
   205 	QString apiKey;
       
   206 	QString apiSecret;
       
   207 	QString authToken;
       
   208 	fetchKeys(apiKey, apiSecret, authToken );
       
   209 	
       
   210 	// Create the API signature string
       
   211 	QString baseString;
       
   212 	baseString.append(apiSecret);
       
   213 	baseString.append("api_key"+apiKey);
       
   214 	baseString.append("auth_token"+authToken);
       
   215 	baseString.append("filterfriends");
       
   216 #ifdef SMF_XMLPARSING
       
   217 	baseString.append("formatxml");
       
   218 #else
       
   219 	baseString.append("formatjson");
       
   220 #endif
       
   221 	baseString.append("methodflickr.contacts.getList");
       
   222 	baseString.append("page"+QString::number(aPageNum));
       
   223 	baseString.append("per_page"+QString::number(aItemsPerPage));
       
   224 	
       
   225 	// Create the url
       
   226 	QUrl url("http://api.flickr.com/services/rest/?");
       
   227 	url.addQueryItem("api_key", apiKey);
       
   228 	url.addQueryItem("auth_token", authToken);
       
   229 	url.addQueryItem("filter", "friends");
       
   230 #ifdef SMF_XMLPARSING
       
   231 	url.addQueryItem("format", "x");
       
   232 #else
       
   233 	url.addQueryItem("format", "json");
       
   234 #endif
       
   235 	url.addQueryItem("method", "flickr.contacts.getList");
       
   236 	url.addQueryItem("page", QString::number(aPageNum));
       
   237 	url.addQueryItem("per_page", QString::number(aItemsPerPage));
       
   238 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   239 	
       
   240 	// Create the request, set the url
       
   241 	aRequest.iNetworkRequest.setUrl(url);
       
   242 	aRequest.iRequestType = SmfContactGetFollowers;
       
   243 	aRequest.iPostData = NULL;
       
   244 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   245 	error = SmfPluginErrNone;
       
   246 
       
   247 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   248 	return error; 
       
   249 #endif
   191 	}
   250 	}
   192 
   251 
   193 /**
   252 /**
   194  * Method to search for a contact
   253  * Method to search for a contact
   195  * @param aRequest [out] The request data to be sent to network
   254  * @param aRequest [out] The request data to be sent to network
   201 SmfPluginError FlickrContactFetcherPlugin::search( SmfPluginRequestData &aRequest,
   260 SmfPluginError FlickrContactFetcherPlugin::search( SmfPluginRequestData &aRequest,
   202 		const SmfContact &aContact,
   261 		const SmfContact &aContact,
   203 		const int aPageNum , 
   262 		const int aPageNum , 
   204 		const int aItemsPerPage  )
   263 		const int aItemsPerPage  )
   205 	{
   264 	{
       
   265 #ifndef TESTINGTHISFUNCTION
   206 	Q_UNUSED(aRequest)
   266 	Q_UNUSED(aRequest)
   207 	Q_UNUSED(aContact)
   267 	Q_UNUSED(aContact)
   208 	Q_UNUSED(aPageNum)
   268 	Q_UNUSED(aPageNum)
   209 	Q_UNUSED(aItemsPerPage)
   269 	Q_UNUSED(aItemsPerPage)
   210 	qDebug()<<"Inside FlickrContactFetcherPlugin::search()";
   270 	qDebug()<<"Inside FlickrContactFetcherPlugin::search()";
   211 	return SmfPluginErrServiceNotSupported; 
   271 	return SmfPluginErrServiceNotSupported; 
       
   272 #else
       
   273 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   274 	
       
   275 	// Get the key sets from SMF Plugin Utility class.
       
   276 	QString apiKey;
       
   277 	QString apiSecret;
       
   278 	QString authToken;
       
   279 	fetchKeys(apiKey, apiSecret, authToken );
       
   280 	
       
   281 	// Create the API signature string
       
   282 	QString baseString;
       
   283 	baseString.append(apiSecret);
       
   284 	baseString.append("api_key"+apiKey);
       
   285 	baseString.append("auth_token"+authToken);
       
   286 	baseString.append("formatjson");
       
   287 	baseString.append("methodflickr.people.findByUsername");
       
   288 	//baseString.append("page"+QString::number(aPageNum));
       
   289 	//baseString.append("per_page"+QString::number(aItemsPerPage));
       
   290 	baseString.append("username"+(aContact.value("Name").value<QContactName>().firstName()));
       
   291 	
       
   292 	
       
   293 	// Create the url
       
   294 	QUrl url("http://api.flickr.com/services/rest/?");
       
   295 	url.addQueryItem("api_key", apiKey);
       
   296 	url.addQueryItem("auth_token", authToken);
       
   297 	url.addQueryItem("format", "json");
       
   298 	url.addQueryItem("method", "flickr.people.findByUsername");
       
   299 	url.addQueryItem("username",aContact.value("Name").value<QContactName>().firstName());
       
   300 	//url.addQueryItem("page", QString::number(10));
       
   301 	//url.addQueryItem("per_page", QString::number(10));
       
   302 
       
   303 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   304 	
       
   305 	// Create the request, set the url
       
   306 	aRequest.iNetworkRequest.setUrl(url);
       
   307 	aRequest.iRequestType = SmfContactSearch;
       
   308 	aRequest.iPostData = NULL;
       
   309 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   310 	error = SmfPluginErrNone;
       
   311 //	writeLog("Url string is :"+aRequest.iNetworkRequest.url().toString());
       
   312 	return error; 
       
   313 
       
   314 #endif
   212 	}
   315 	}
   213 
   316 
   214 /**
   317 /**
   215  * Method to search for contacts (friends) who are near the user.
   318  * Method to search for contacts (friends) who are near the user.
   216  * Proximity defines accuracy level
   319  * Proximity defines accuracy level
   225 		const SmfLocation &aLocation,
   328 		const SmfLocation &aLocation,
   226 		const SmfLocationSearchBoundary &aProximity,
   329 		const SmfLocationSearchBoundary &aProximity,
   227 		const int aPageNum , 
   330 		const int aPageNum , 
   228 		const int aItemsPerPage  )
   331 		const int aItemsPerPage  )
   229 	{
   332 	{
       
   333 #ifndef TESTINGTHISFUNCTION
   230 	Q_UNUSED(aRequest)
   334 	Q_UNUSED(aRequest)
   231 	Q_UNUSED(aLocation)
   335 	Q_UNUSED(aLocation)
   232 	Q_UNUSED(aProximity)
   336 	Q_UNUSED(aProximity)
   233 	Q_UNUSED(aPageNum)
   337 	Q_UNUSED(aPageNum)
   234 	Q_UNUSED(aItemsPerPage)
   338 	Q_UNUSED(aItemsPerPage)
   235 	qDebug()<<"Inside FlickrContactFetcherPlugin::searchNear()";
   339 	qDebug()<<"Inside FlickrContactFetcherPlugin::searchNear()";
   236 	return SmfPluginErrServiceNotSupported; 
   340 	return SmfPluginErrServiceNotSupported; 
       
   341 #else
       
   342 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   343 
       
   344 	// invalid arguments
       
   345 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
   346 		{
       
   347 		qDebug()<<"Invalid arguments";
       
   348 		return error;
       
   349 		}
       
   350 	
       
   351 	qDebug()<<"Valid arguments";
       
   352 	
       
   353 	// Get the key sets from SMF Plugin Utility class.
       
   354 	QString apiKey;
       
   355 	QString apiSecret;
       
   356 	QString authToken;
       
   357 	fetchKeys(apiKey, apiSecret, authToken );
       
   358 	
       
   359 	// Create the API signature string
       
   360 	QString baseString;
       
   361 	baseString.append(apiSecret);
       
   362 	baseString.append("api_key"+apiKey);
       
   363 	baseString.append("auth_token"+authToken);
       
   364 	baseString.append("filterfriends");
       
   365 #ifdef SMF_XMLPARSING
       
   366 	baseString.append("formatxml");
       
   367 #else
       
   368 	baseString.append("formatjson");
       
   369 #endif
       
   370 	baseString.append("methodflickr.contacts.getList");
       
   371 	baseString.append("page"+QString::number(aPageNum));
       
   372 	baseString.append("per_page"+QString::number(aItemsPerPage));
       
   373 	
       
   374 	// Create the url
       
   375 	QUrl url("http://api.flickr.com/services/rest/?");
       
   376 	url.addQueryItem("api_key", apiKey);
       
   377 	url.addQueryItem("auth_token", authToken);
       
   378 	url.addQueryItem("filter", "friends");
       
   379 #ifdef SMF_XMLPARSING
       
   380 	url.addQueryItem("format", "x");
       
   381 #else
       
   382 	url.addQueryItem("format", "json");
       
   383 #endif
       
   384 	url.addQueryItem("method", "flickr.contacts.getList");
       
   385 	url.addQueryItem("page", QString::number(aPageNum));
       
   386 	url.addQueryItem("per_page", QString::number(aItemsPerPage));
       
   387 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   388 	
       
   389 	// Create the request, set the url
       
   390 	aRequest.iNetworkRequest.setUrl(url);
       
   391 	aRequest.iRequestType = SmfContactSearchNear;
       
   392 	aRequest.iPostData = NULL;
       
   393 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   394 	error = SmfPluginErrNone;
       
   395 
       
   396 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   397 	return error; 
       
   398 #endif
   237 	}
   399 	}
   238 
   400 
   239 /**
   401 /**
   240  * Method to get the list of groups
   402  * Method to get the list of groups
   241  * @param aRequest [out] The request data to be sent to network
   403  * @param aRequest [out] The request data to be sent to network
   245  */
   407  */
   246 SmfPluginError FlickrContactFetcherPlugin::groups( SmfPluginRequestData &aRequest,
   408 SmfPluginError FlickrContactFetcherPlugin::groups( SmfPluginRequestData &aRequest,
   247 		const int aPageNum , 
   409 		const int aPageNum , 
   248 		const int aItemsPerPage  )
   410 		const int aItemsPerPage  )
   249 	{
   411 	{
   250 	Q_UNUSED(aRequest)
       
   251 	Q_UNUSED(aPageNum)
       
   252 	Q_UNUSED(aItemsPerPage)
       
   253 	qDebug()<<"Inside FlickrContactFetcherPlugin::groups()";
   412 	qDebug()<<"Inside FlickrContactFetcherPlugin::groups()";
   254 	return SmfPluginErrServiceNotSupported; 
   413 
       
   414 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   415 
       
   416 	// invalid arguments
       
   417 		/*if( aPageNum < 0 || aItemsPerPage < 0 )
       
   418 			{
       
   419 			qDebug()<<"Invalid arguments";
       
   420 			return error;
       
   421 			}*/
       
   422 	// Get the key sets from SMF Plugin Utility class.
       
   423 	QString apiKey;
       
   424 	QString apiSecret;
       
   425 	QString authToken;
       
   426 	fetchKeys(apiKey, apiSecret, authToken );
       
   427 	
       
   428 	// Create the API signature string
       
   429 	QString baseString;
       
   430 	baseString.append(apiSecret);
       
   431 	baseString.append("api_key"+apiKey);
       
   432 	baseString.append("auth_token"+authToken);
       
   433 	baseString.append("formatjson");
       
   434 	baseString.append("methodflickr.groups.pools.getGroups");
       
   435 	
       
   436 	// Create the url
       
   437 	QUrl url("http://api.flickr.com/services/rest/?");
       
   438 	url.addQueryItem("api_key", apiKey);
       
   439 	url.addQueryItem("auth_token", authToken);
       
   440 	url.addQueryItem("format", "json");
       
   441 	url.addQueryItem("method","flickr.groups.pools.getGroups");
       
   442 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   443 		
       
   444 	// Create the request, set the url
       
   445 	aRequest.iNetworkRequest.setUrl(url);
       
   446 	aRequest.iRequestType = SmfContactGetGroups;
       
   447 	aRequest.iPostData = NULL;
       
   448 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   449 	error = SmfPluginErrNone;
       
   450 //	writeLog("Url string is : "+aRequest.iNetworkRequest.url().toString());
       
   451 	return error; 
   255 	}
   452 	}
   256 
   453 
   257 /**
   454 /**
   258  * Method to search for a contact in a group
   455  * Method to search for a contact in a group
   259  * @param aRequest [out] The request data to be sent to network
   456  * @param aRequest [out] The request data to be sent to network
   260  * @param aGroup the group in which to search
   457  * @param aGroup the group in which to search
       
   458  * @param aContact The contact to be searched, default (NULL) is the self contact.
   261  * @param aPageNum The page to be extracted
   459  * @param aPageNum The page to be extracted
   262  * @param aItemsPerPage Number of items per page
   460  * @param aItemsPerPage Number of items per page
   263  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
   461  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
   264  */
   462  */
   265 SmfPluginError FlickrContactFetcherPlugin::searchInGroup( SmfPluginRequestData &aRequest,
   463 SmfPluginError FlickrContactFetcherPlugin::searchInGroup( SmfPluginRequestData &aRequest,
   266 		const SmfGroup &aGroup,
   464 		const SmfGroup &aGroup,
       
   465 		SmfContact *aContact,
   267 		const int aPageNum , 
   466 		const int aPageNum , 
   268 		const int aItemsPerPage  )
   467 		const int aItemsPerPage  )
   269 	{
   468 	{
       
   469 #ifndef TESTINGTHISFUNCTION
   270 	Q_UNUSED(aRequest)
   470 	Q_UNUSED(aRequest)
   271 	Q_UNUSED(aGroup)
   471 	Q_UNUSED(aGroup)
   272 	Q_UNUSED(aPageNum)
   472 	Q_UNUSED(aPageNum)
   273 	Q_UNUSED(aItemsPerPage)
   473 	Q_UNUSED(aItemsPerPage)
   274 	qDebug()<<"Inside FlickrContactFetcherPlugin::searchInGroup()";
   474 	qDebug()<<"Inside FlickrContactFetcherPlugin::searchInGroup()";
   275 	return SmfPluginErrServiceNotSupported; 
   475 	return SmfPluginErrServiceNotSupported; 
       
   476 #else
       
   477 	SmfPluginError error = SmfPluginErrInvalidArguments;
       
   478 
       
   479 	// invalid arguments
       
   480 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
   481 		{
       
   482 		qDebug()<<"Invalid arguments";
       
   483 		return error;
       
   484 		}
       
   485 	
       
   486 	qDebug()<<"Valid arguments";
       
   487 	
       
   488 	// Get the key sets from SMF Plugin Utility class.
       
   489 	QString apiKey;
       
   490 	QString apiSecret;
       
   491 	QString authToken;
       
   492 	fetchKeys(apiKey, apiSecret, authToken );
       
   493 	
       
   494 	// Create the API signature string
       
   495 	QString baseString;
       
   496 	baseString.append(apiSecret);
       
   497 	baseString.append("api_key"+apiKey);
       
   498 	baseString.append("auth_token"+authToken);
       
   499 	baseString.append("filterfriends");
       
   500 #ifdef SMF_XMLPARSING
       
   501 	baseString.append("formatxml");
       
   502 #else
       
   503 	baseString.append("formatjson");
       
   504 #endif
       
   505 	baseString.append("methodflickr.contacts.getList");
       
   506 	baseString.append("page"+QString::number(aPageNum));
       
   507 	baseString.append("per_page"+QString::number(aItemsPerPage));
       
   508 	
       
   509 	// Create the url
       
   510 	QUrl url("http://api.flickr.com/services/rest/?");
       
   511 	url.addQueryItem("api_key", apiKey);
       
   512 	url.addQueryItem("auth_token", authToken);
       
   513 	url.addQueryItem("filter", "friends");
       
   514 #ifdef SMF_XMLPARSING
       
   515 	url.addQueryItem("format", "x");
       
   516 #else
       
   517 	url.addQueryItem("format", "json");
       
   518 #endif
       
   519 	url.addQueryItem("method", "flickr.contacts.getList");
       
   520 	url.addQueryItem("page", QString::number(aPageNum));
       
   521 	url.addQueryItem("per_page", QString::number(aItemsPerPage));
       
   522 	url.addQueryItem("api_sig", generateSignature(baseString));
       
   523 	
       
   524 	// Create the request, set the url
       
   525 	aRequest.iNetworkRequest.setUrl(url);
       
   526 	aRequest.iRequestType = SmfContactSearchInGroup;
       
   527 	aRequest.iPostData = NULL;
       
   528 	aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   529 	error = SmfPluginErrNone;
       
   530 
       
   531 	qDebug()<<"Url string is : "<<aRequest.iNetworkRequest.url().toString();
       
   532 	return error; 
       
   533 #endif
   276 	}
   534 	}
   277 
   535 
   278 /**
   536 /**
   279  * Customised method for SmfContactFetcherPlugin interface
   537  * Customised method for SmfContactFetcherPlugin interface
   280  * @param aRequest [out] The request data to be sent to network
   538  * @param aRequest [out] The request data to be sent to network
   370 	
   628 	
   371 	if(SmfTransportOpNoError == aTransportResult)
   629 	if(SmfTransportOpNoError == aTransportResult)
   372 		{
   630 		{
   373 		qDebug()<<"No transport error";
   631 		qDebug()<<"No transport error";
   374 		
   632 		
       
   633 #ifndef TESTINGTHISFUNCTION	
   375 		if(SmfContactGetFriends == aOperation)
   634 		if(SmfContactGetFriends == aOperation)
       
   635 #else
       
   636 		if(SmfContactGetFriends == aOperation ||aOperation == SmfContactGetFollowers||aOperation== SmfContactSearch ||aOperation ==SmfContactSearchNear||aOperation ==SmfContactSearchInGroup)
       
   637 #endif
   376 			{
   638 			{
   377 			qDebug()<<"For getting friends response";
   639 			qDebug()<<"For getting friends response";
   378 			
   640 			
   379 			QList<SmfContact> list;
   641 			QList<SmfContact> list;
   380 			
   642 			
   489 			qDebug()<<"list count = "<<list.count();
   751 			qDebug()<<"list count = "<<list.count();
   490 			aResult->setValue(list);
   752 			aResult->setValue(list);
   491 			aRetType = SmfRequestComplete;
   753 			aRetType = SmfRequestComplete;
   492 			error = SmfPluginErrNone;
   754 			error = SmfPluginErrNone;
   493 			}
   755 			}
       
   756 		else if(aOperation==SmfContactGetGroups)
       
   757 			{
       
   758 		        response.remove(0, 14);
       
   759 				response.chop(1);
       
   760 			
       
   761 			    bool ok;
       
   762 				qDebug()<<"Before Parser--";
       
   763 
       
   764 				SmfPluginUtil util;
       
   765 				QVariant result = util.parse(response, &ok);
       
   766 				if (!ok) 
       
   767 				{
       
   768 				    qDebug()<<"An error occurred during json parsing";
       
   769 					aRetType = SmfRequestError;
       
   770 					return SmfPluginErrParsingFailed;
       
   771 								 //return 0;
       
   772 				}
       
   773 							
       
   774 				QVariantMap map1 = result.toMap();
       
   775 		        QList<SmfGroup> list;
       
   776 				QVariantMap map2 = map1["groups"].toMap();
       
   777 				int page = map2["page"].toInt(&ok);
       
   778 				qDebug()<<"PAGE = "<<map2["page"].toString();
       
   779 				QList<QVariant> list1 = map2["group"].toList();
       
   780 				QListIterator<QVariant> iter(list1);
       
   781 						
       
   782 				//Getting the group list from QJson Parser 
       
   783 				 while(iter.hasNext())
       
   784 				 {
       
   785 					QVariantMap map2 = iter.next().toMap();
       
   786 					qDebug()<<"name = "<<map2["name"].toString();
       
   787 					qDebug()<<"id = "<<map2["id"].toString();
       
   788 					SmfGroup group;
       
   789 					QString id(map2["id"].toString());
       
   790 					group.setId(id);
       
   791 					QString grpname(map2["name"].toString());
       
   792 					group.setName(grpname);
       
   793 						   
       
   794 					list.append(group);
       
   795 						   
       
   796 				 }//end While
       
   797 				 qDebug()<<"list count = "<<QString::number(list.count(),10);
       
   798 				aResult->setValue(list);
       
   799 			}
   494 		else
   800 		else
   495 			{
   801 			{
   496 			qDebug()<<"Service unsupported, currently only SmfContactGetFriends !!!";
   802 			qDebug()<<"Service unsupported, currently only SmfContactGetFriends !!!";
   497 			aRetType = SmfRequestError;
   803 			aRetType = SmfRequestError;
   498 			error = SmfPluginErrServiceNotSupported;
   804 			error = SmfPluginErrServiceNotSupported;