example/flickrcontactfetcherplugin/flickrcontactfetcherplugin.cpp
changeset 6 c39a6cfd1fb9
equal deleted inserted replaced
5:edb9dc8273d9 6:c39a6cfd1fb9
       
     1 
       
     2 // Include files
       
     3 #include <QtPlugin>
       
     4 #include <QCryptographicHash>
       
     5 #include <QDataStream>
       
     6 #include <QTextStream>
       
     7 #include <QFile>
       
     8 #include <QNetworkReply>
       
     9 #include <QXmlStreamReader>
       
    10 #include <parser.h>
       
    11 #include <QMap>
       
    12 #include <QListIterator>
       
    13 
       
    14 #include "flickrcontactfetcherplugin.h"
       
    15 
       
    16 // HARD CODED AS CSM IS NOT AVAILABLE - START - use your rigistered app's keys here
       
    17 static const QString apiKey = "";
       
    18 static const QString apiSecret = "";
       
    19 static const QString miniToken = "";
       
    20 QString fullToken = "";
       
    21 // HARD CODED AS CSM IS NOT AVAILABLE - END
       
    22 
       
    23 
       
    24 /**
       
    25  * Method called by plugins for logging
       
    26  * @param log string to be logged
       
    27  */
       
    28 void FlickrContactFetcherPlugin::writeLog(QString log) const
       
    29 	{
       
    30 	QFile file("c:\\data\\PluginLogs.txt");
       
    31     if (!file.open(QIODevice::Append | QIODevice::Text))
       
    32 	         ;
       
    33     QTextStream out(&file);
       
    34     out << log << "\n";
       
    35     file.close();
       
    36 	}
       
    37 
       
    38 /**
       
    39  * Destructor
       
    40  */
       
    41 FlickrContactFetcherPlugin::~FlickrContactFetcherPlugin( )
       
    42 	{
       
    43 	if(m_provider)
       
    44 		delete m_provider;
       
    45 	}
       
    46 
       
    47 /**
       
    48  * Method to get the list of friends
       
    49  * @param aRequest [out] The request data to be sent to network
       
    50  * @param aPageNum The page to be extracted
       
    51  * @param aItemsPerPage Number of items per page
       
    52  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
    53  */
       
    54 SmfPluginError FlickrContactFetcherPlugin::friends( SmfPluginRequestData &aRequest,
       
    55 		const int aPageNum,
       
    56 		const int aItemsPerPage )
       
    57 	{
       
    58 	writeLog("FlickrContactFetcherPlugin::friends");
       
    59 
       
    60 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
    61 
       
    62 	// invalid arguments
       
    63 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
    64 		return error;
       
    65 	else
       
    66 		{
       
    67 		// Create the API signature string
       
    68 		QString baseString;
       
    69 		baseString.append(apiSecret);
       
    70 		baseString.append("api_key"+apiKey);
       
    71 		baseString.append("auth_token"+fullToken);
       
    72 		baseString.append("filterfriends");
       
    73 		baseString.append("formatjson");
       
    74 		baseString.append("methodflickr.contacts.getList");
       
    75 		baseString.append("page"+QString::number(aPageNum));
       
    76 		baseString.append("per_page"+QString::number(aItemsPerPage));
       
    77 
       
    78 		// Create the url
       
    79 		QUrl url("http://api.flickr.com/services/rest/?");
       
    80 		url.addQueryItem("method", "flickr.contacts.getList");
       
    81 		url.addQueryItem("api_key", apiKey);
       
    82 		url.addQueryItem("filter", "friends");
       
    83 		url.addQueryItem("format", "json");
       
    84 		url.addQueryItem("page", QString::number(aPageNum));
       
    85 		url.addQueryItem("per_page", QString::number(aItemsPerPage));
       
    86 		url.addQueryItem("auth_token", fullToken);
       
    87 		url.addQueryItem("api_sig", generateSignature(baseString));
       
    88 
       
    89 		// Create the request, set the url
       
    90 		writeLog("final url = "+url.toString());
       
    91 		aRequest.iNetworkRequest.setUrl(url);
       
    92 		aRequest.iRequestType = SmfContactGetFriends;
       
    93 		aRequest.iPostData = NULL;
       
    94 		aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
    95 		error = SmfPluginErrNone;
       
    96 		}
       
    97 	writeLog("Url string is : "+aRequest.iNetworkRequest.url().toString());
       
    98 	return error;
       
    99 	}
       
   100 
       
   101 /**
       
   102  * Method called by plugins to generate a signature string from a base string
       
   103  * @param aBaseString The base string
       
   104  * @return The md5 hash of the base string
       
   105  */
       
   106 QString FlickrContactFetcherPlugin::generateSignature(const QString aBaseString)
       
   107 	{
       
   108 	writeLog("FlickrContactFetcherPlugin::generateSignature");
       
   109 
       
   110 	// Create md5 hash of the signature string
       
   111     QByteArray byteArray;
       
   112     byteArray.insert(0, aBaseString.toAscii());
       
   113 
       
   114     QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex();
       
   115     QString returnString (md5Hash);
       
   116     writeLog("generated signature = "+QString(returnString));
       
   117     return returnString;
       
   118 	}
       
   119 
       
   120 /**
       
   121  * Method to get the list of followers
       
   122  * @param aRequest [out] The request data to be sent to network
       
   123  * @param aPageNum The page to be extracted
       
   124  * @param aItemsPerPage Number of items per page
       
   125  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   126  */
       
   127 SmfPluginError FlickrContactFetcherPlugin::followers( SmfPluginRequestData &aRequest,
       
   128 		const int aPageNum ,
       
   129 		const int aItemsPerPage  )
       
   130 	{
       
   131 	Q_UNUSED(aRequest)
       
   132 Q_UNUSED(aPageNum)
       
   133 Q_UNUSED(aItemsPerPage)
       
   134 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   135 	return error;
       
   136 	}
       
   137 
       
   138 /**
       
   139  * Method to search for a contact
       
   140  * @param aRequest [out] The request data to be sent to network
       
   141  * @param aContact contact to be searched
       
   142  * @param aPageNum The page to be extracted
       
   143  * @param aItemsPerPage Number of items per page
       
   144  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   145  */
       
   146 SmfPluginError FlickrContactFetcherPlugin::search( SmfPluginRequestData &aRequest,
       
   147 		const SmfContact &aContact,
       
   148 		const int aPageNum ,
       
   149 		const int aItemsPerPage  )
       
   150 	{
       
   151 	Q_UNUSED(aRequest)
       
   152 Q_UNUSED(aContact)
       
   153 Q_UNUSED(aPageNum)
       
   154 Q_UNUSED(aItemsPerPage)
       
   155 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   156 	return error;
       
   157 	}
       
   158 
       
   159 /**
       
   160  * Method to search for contacts (friends) who are near the user.
       
   161  * Proximity defines accuracy level
       
   162  * @param aRequest [out] The request data to be sent to network
       
   163  * @param aLocation The location search criteria
       
   164  * @param aProximity location search boundary
       
   165  * @param aPageNum The page to be extracted
       
   166  * @param aItemsPerPage Number of items per page
       
   167  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   168  */
       
   169 SmfPluginError FlickrContactFetcherPlugin::searchNear( SmfPluginRequestData &aRequest,
       
   170 		const SmfLocation &aLocation,
       
   171 		const SmfLocationSearchBoundary &aProximity,
       
   172 		const int aPageNum ,
       
   173 		const int aItemsPerPage  )
       
   174 	{
       
   175 	Q_UNUSED(aRequest)
       
   176 Q_UNUSED(aLocation)
       
   177 Q_UNUSED(aProximity)
       
   178 Q_UNUSED(aPageNum)
       
   179 Q_UNUSED(aItemsPerPage)
       
   180 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   181 	return error;
       
   182 	}
       
   183 
       
   184 /**
       
   185  * Method to get the list of groups
       
   186  * @param aRequest [out] The request data to be sent to network
       
   187  * @param aPageNum The page to be extracted
       
   188  * @param aItemsPerPage Number of items per page
       
   189  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   190  */
       
   191 SmfPluginError FlickrContactFetcherPlugin::groups( SmfPluginRequestData &aRequest,
       
   192 		const int aPageNum ,
       
   193 		const int aItemsPerPage  )
       
   194 	{
       
   195 	Q_UNUSED(aRequest)
       
   196 Q_UNUSED(aPageNum)
       
   197 Q_UNUSED(aItemsPerPage)
       
   198 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   199 	return error;
       
   200 	}
       
   201 
       
   202 /**
       
   203  * Method to search for a contact in a group
       
   204  * @param aRequest [out] The request data to be sent to network
       
   205  * @param aGroup the group in which to search
       
   206  * @param aPageNum The page to be extracted
       
   207  * @param aItemsPerPage Number of items per page
       
   208  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   209  */
       
   210 SmfPluginError FlickrContactFetcherPlugin::searchInGroup( SmfPluginRequestData &aRequest,
       
   211 		const SmfGroup &aGroup,
       
   212 		const int aPageNum ,
       
   213 		const int aItemsPerPage  )
       
   214 	{
       
   215 	Q_UNUSED(aRequest)
       
   216 Q_UNUSED(aGroup)
       
   217 Q_UNUSED(aPageNum)
       
   218 Q_UNUSED(aItemsPerPage)
       
   219 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   220 	return error;
       
   221 	}
       
   222 
       
   223 /**
       
   224  * Customised method for SmfContactFetcherPlugin interface
       
   225  * @param aRequest [out] The request data to be sent to network
       
   226  * @param aOperation The operation type (should be known between
       
   227  * the client interface and the plugin)
       
   228  * @param aData The data required to form the request (The type
       
   229  * of data should be known between client and the plugin)
       
   230  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   231  */
       
   232 SmfPluginError FlickrContactFetcherPlugin::customRequest( SmfPluginRequestData &aRequest,
       
   233 		const int &aOperation, QByteArray *aData )
       
   234 	{
       
   235 	Q_UNUSED(aRequest)
       
   236 Q_UNUSED(aOperation)
       
   237 Q_UNUSED(aData)
       
   238 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   239 	return error;
       
   240 	}
       
   241 
       
   242 /**
       
   243  * The first method to be called in the plugin that implements this interface.
       
   244  * If this method is not called, plugin may not behave as expected.
       
   245  * Plugins are expected to save the aUtil handle and use and when required.
       
   246  * @param aUtil The instance of SmfPluginUtil
       
   247  */
       
   248 void FlickrContactFetcherPlugin::initialize( SmfPluginUtil *aUtil )
       
   249 	{
       
   250 	// Save the SmfPluginUtil handle
       
   251 	m_util = aUtil;
       
   252 
       
   253 	// Create an instance of FlickrProviderBase
       
   254 	m_provider = new FlickrProviderBase;
       
   255 	m_provider->initialize();
       
   256 	}
       
   257 
       
   258 /**
       
   259  * Method to get the provider information
       
   260  * @return Instance of SmfProviderBase
       
   261  */
       
   262 SmfProviderBase* FlickrContactFetcherPlugin::getProviderInfo( )
       
   263 	{
       
   264 	return m_provider;
       
   265 	}
       
   266 
       
   267 /**
       
   268  * Method to get the result for a network request.
       
   269  * @param aTransportResult The result of transport operation
       
   270  * @param aResponse The QByteArray instance containing the network response.
       
   271  * The plugins should delete this instance once they have read the
       
   272  * data from it.
       
   273  * @param aResult [out] An output parameter to the plugin manager.If the
       
   274  * return value is SmfSendRequestAgain, QVariant will be of type
       
   275  * SmfPluginRequestData.
       
   276  * For SmfGalleryPlugin: If last operation was pictures(), aResult will
       
   277  * be of type QList<SmfPicture>. If last operation was description(),
       
   278  * aResult will be of type QString. If last operation was upload() or
       
   279  * postComment(), aResult will be of type bool.
       
   280  * @param aRetType [out] SmfPluginRetType
       
   281  * @param aPageResult [out] The SmfResultPage structure variable
       
   282  */
       
   283 SmfPluginError FlickrContactFetcherPlugin::responseAvailable(
       
   284 		const SmfTransportResult &aTransportResult,
       
   285 		QByteArray *aResponse,
       
   286 		QVariant* aResult,
       
   287 		SmfPluginRetType &aRetType,
       
   288 		SmfResultPage &aPageResult )
       
   289 	{
       
   290 	writeLog("FlickrContactFetcherPlugin::responseAvailable");
       
   291 	Q_UNUSED(aPageResult)
       
   292 	SmfPluginError error;
       
   293 	QList<SmfContact> list;
       
   294 
       
   295 	if(SmfTransportOpNoError == aTransportResult)
       
   296 		{
       
   297 		writeLog("No transport error");
       
   298 
       
   299 		QByteArray response(aResponse->data());
       
   300 		delete aResponse;
       
   301 		writeLog("Flickr response = "+QString(response));
       
   302 
       
   303 #if 1
       
   304 		// For getting contacts from json response
       
   305 		QJson::Parser parser;
       
   306 		bool ok;
       
   307 
       
   308 		// To remove the "jsonFlickrApi(" and also remove the last ")" from the response,
       
   309 		// as these gives a Json parsing error
       
   310 		response.remove(0, 14);
       
   311 		response.chop(1);
       
   312 
       
   313 		QVariantMap result = parser.parse(response, &ok).toMap();
       
   314 		if (!ok) {
       
   315 			writeLog("An error occurred during json parsing");
       
   316 			aResult->setValue(list);
       
   317 			error = SmfPluginErrParsingFailed;
       
   318 			return error;
       
   319 		}
       
   320 
       
   321 		QVariantMap map1 = result["contacts"].toMap();
       
   322 		writeLog("page = "+map1["page"].toString());
       
   323 		writeLog("pages = "+map1["pages"].toString());
       
   324 		writeLog("per_page = "+map1["per_page"].toString());
       
   325 		writeLog("perpage = "+map1["perpage"].toString());
       
   326 		writeLog("total = "+map1["perpage"].toString());
       
   327 
       
   328 		QList<QVariant> list1 = map1["contact"].toList();
       
   329 
       
   330 		QListIterator<QVariant> i(list1);
       
   331 		while(i.hasNext())
       
   332 			{
       
   333 			SmfContact contact;
       
   334 			QVariantMap map2 = i.next().toMap();
       
   335 			writeLog("nsid = "+map2["nsid"].toString());
       
   336 			writeLog("username = "+map2["username"].toString());
       
   337 			writeLog("iconserver = "+map2["iconserver"].toString());
       
   338 			writeLog("iconfarm = "+map2["iconfarm"].toString());
       
   339 			writeLog("ignored = "+map2["ignored"].toString());
       
   340 			writeLog("realname = "+map2["realname"].toString());
       
   341 			writeLog("friend = "+map2["friend"].toString());
       
   342 			writeLog("family = "+map2["family"].toString());
       
   343 			writeLog("path_alias = "+map2["path_alias"].toString());
       
   344 			writeLog("location = "+map2["location"].toString());
       
   345 
       
   346 			QContactName contactname;
       
   347 			QString username = map2["username"].toString();
       
   348 			writeLog("Username = "+username);
       
   349 			contactname.setFirstName(username);
       
   350 			contactname.setLastName(username);
       
   351 			QVariant nameVar = QVariant::fromValue(contactname);
       
   352 			contact.setValue("Name",nameVar);
       
   353 			list.append(contact);
       
   354 			}
       
   355 #endif
       
   356 
       
   357 #if 0
       
   358 		// For getting contacts from xml response
       
   359 		QXmlStreamReader xml(response);
       
   360 		while (!xml.atEnd())
       
   361 			{
       
   362 			xml.readNext();
       
   363 			if (xml.tokenType() == QXmlStreamReader::StartElement)
       
   364 				{
       
   365 				// If the tag is contact
       
   366 				if (xml.name() == "contact")
       
   367 					{
       
   368 					writeLog("Contact tag found");
       
   369 					SmfContact contact;
       
   370 					QStringRef str;
       
   371 					QContactName contactname;
       
   372 					QString username = xml.attributes().value("username").toString();
       
   373 					writeLog("Username = ");
       
   374 					writeLog(username);
       
   375 					contactname.setFirstName(username);
       
   376 					contactname.setLastName(username);
       
   377 					QVariant namevar1 = QVariant::fromValue(contactname);
       
   378 					contact.setValue("Name",namevar1);
       
   379                     list.append(contact);
       
   380 					}
       
   381 				}
       
   382 			}
       
   383 #endif
       
   384 
       
   385 		writeLog("list count = "+QString::number(list.count(),10));
       
   386 		aRetType = SmfRequestComplete;
       
   387 		error = SmfPluginErrNone;
       
   388 		}
       
   389 
       
   390 	else
       
   391 		{
       
   392 		error = SmfPluginErrInvalidRequest;
       
   393 		aRetType = SmfRequestError;
       
   394 		}
       
   395 
       
   396 	aResult->setValue(list);
       
   397 	return error;
       
   398 	}
       
   399 
       
   400 
       
   401 /**
       
   402  * Destructor
       
   403  */
       
   404 FlickrProviderBase::~FlickrProviderBase( )
       
   405 	{
       
   406 	}
       
   407 
       
   408 /**
       
   409  * Method to get the Localisable name of the service.
       
   410  * @return The Localisable name of the service.
       
   411  */
       
   412 QString FlickrProviderBase::serviceName( ) const
       
   413 	{
       
   414 	return m_serviceName;
       
   415 	}
       
   416 
       
   417 /**
       
   418  * Method to get the Logo of the service
       
   419  * @return The Logo of the service
       
   420  */
       
   421 QImage FlickrProviderBase::serviceIcon( ) const
       
   422 	{
       
   423 	return m_serviceIcon;
       
   424 	}
       
   425 
       
   426 /**
       
   427  * Method to get the Readable service description
       
   428  * @return The Readable service description
       
   429  */
       
   430 QString FlickrProviderBase::description( ) const
       
   431 	{
       
   432 	return m_description;
       
   433 	}
       
   434 
       
   435 /**
       
   436  * Method to get the Website of the service
       
   437  * @return The Website of the service
       
   438  */
       
   439 QUrl FlickrProviderBase::serviceUrl( ) const
       
   440 	{
       
   441 	return m_serviceUrl;
       
   442 	}
       
   443 
       
   444 /**
       
   445  * Method to get the URL of the Application providing this service
       
   446  * @return The URL of the Application providing this service
       
   447  */
       
   448 QUrl FlickrProviderBase::applicationUrl( ) const
       
   449 	{
       
   450 	return m_applicationUrl;
       
   451 	}
       
   452 
       
   453 /**
       
   454  * Method to get the Icon of the application
       
   455  * @return The Icon of the application
       
   456  */
       
   457 QImage FlickrProviderBase::applicationIcon( ) const
       
   458 	{
       
   459 	return m_applicationIcon;
       
   460 	}
       
   461 
       
   462 /**
       
   463  * Method to get the Plugin specific ID
       
   464  * @return The Plugin specific ID
       
   465  */
       
   466 QString FlickrProviderBase::pluginId( ) const
       
   467 	{
       
   468 	return m_pluginId;
       
   469 	}
       
   470 
       
   471 /**
       
   472  * Method to get the ID of the authentication application
       
   473  * for this service
       
   474  * @param aProgram The authentication application name
       
   475  * @param aArguments List of arguments required for authentication app
       
   476  * @param aMode Strting mode for authentication application
       
   477  * @return The ID of the authentication application
       
   478  */
       
   479 QString FlickrProviderBase::authenticationApp( QString &aProgram,
       
   480 		QStringList & aArguments,
       
   481 		QIODevice::OpenModeFlag aMode ) const
       
   482 	{
       
   483 	Q_UNUSED(aProgram)
       
   484 Q_UNUSED(aArguments)
       
   485 Q_UNUSED(aMode)
       
   486 	return m_authAppId;
       
   487 	}
       
   488 
       
   489 /**
       
   490  * Method to get the unique registration ID provided by the
       
   491  * Smf for authorised plugins
       
   492  * @return The unique registration ID/token provided by the Smf for
       
   493  * authorised plugins
       
   494  */
       
   495 QString FlickrProviderBase::smfRegistrationId( ) const
       
   496 	{
       
   497 	return m_smfRegToken;
       
   498 	}
       
   499 
       
   500 void FlickrProviderBase::initialize()
       
   501 	{
       
   502 	m_serviceName = "Flickr";
       
   503 	m_description = "Flickr plugin description";
       
   504 	m_serviceUrl = QUrl(QString("http://api.flickr.com"));
       
   505 	m_pluginId = "flickrcontactfetcherplugin.qtplugin";
       
   506 	m_authAppId = "Flickr AuthAppId";
       
   507 	m_smfRegToken = "Flickr RegToken";
       
   508 	}
       
   509 
       
   510 
       
   511 /*
       
   512  * Export Macro
       
   513  * plugin name : flickrcontactfetcherplugin
       
   514  * plugin class : FlickrContactFetcherPlugin
       
   515  */
       
   516 Q_EXPORT_PLUGIN2( flickrcontactfetcherplugin, FlickrContactFetcherPlugin )
       
   517