diff -r b5d63d5fc252 -r a469c0e6e7fb example/fbcontactfetcherplugin/fbcontactfetcherplugin.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/fbcontactfetcherplugin/fbcontactfetcherplugin.cpp Wed Jun 23 19:51:49 2010 +0530 @@ -0,0 +1,1046 @@ +/** + * Copyright (c) 2010 Sasken Communication Technologies Ltd. + * All rights reserved. + * This component and the accompanying materials are made available + * under the terms of the "Eclipse Public License v1.0" + * which accompanies this distribution, and is available + * at the URL "http://www.eclipse.org/legal/epl-v10.html" + * + * Initial Contributors: + * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution + * + * Contributors: + * Nalina Hariharan + * + * Description: + * The Plugin that fetches contacts from the logged in user's facebook account + * + */ + +// Include files +#include +#include +#include +#include +#include +#include +#include +#ifdef SMF_XMLPARSING +#include +#endif + +#include "fbcontactfetcherplugin.h" + +// Global variables +static int chance = 0; +QString uids; + +#ifdef SMF_XMLPARSING +SmfContact contact; +#endif + + +/** + * Destructor + */ +FBContactFetcherPlugin::~FBContactFetcherPlugin( ) + { + if(m_provider) + delete m_provider; + } + +/** + * Method to get the list of friends + * @param aRequest [out] The request data to be sent to network + * @param aPageNum The page to be extracted + * @param aItemsPerPage Number of items per page + * @return SmfPluginError Plugin error if any, else SmfPluginErrNone + */ +SmfPluginError FBContactFetcherPlugin::friends( SmfPluginRequestData &aRequest, + const int aPageNum, + const int aItemsPerPage ) + { + qDebug()<<"Inside FBContactFetcherPlugin::friends()"; + + if(0 == chance) + return getFriendsUids(aRequest, aPageNum, aItemsPerPage); + else// if(1 == chance) + return getFriendsDetails(aRequest, aPageNum, aItemsPerPage); + + } + +/** + * Method to get Facebook specific UIDs of the user's friends + * @param aRequest [out] The request data to be sent to network + * @param aPageNum The page to be extracted + * @param aItemsPerPage Number of items per page + * @return SmfPluginError Plugin error if any, else SmfPluginErrNone + */ +SmfPluginError FBContactFetcherPlugin::getFriendsUids( SmfPluginRequestData &aRequest, + const int aPageNum, + const int aItemsPerPage ) + { + qDebug()<<"Inside FBContactFetcherPlugin::getFriendsUids()"; + + SmfPluginError error = SmfPluginErrInvalidArguments; + + // invalid arguments + if( aPageNum < 0 || aItemsPerPage < 0 ) + { + qDebug()<<"Invalid arguments"; + return error; + } + + qDebug()<<"Valid arguments"; + +#if 1 +// Reading the keys, CSM Stubbed - START + QFile file("c:\\data\\FacebookKeys.txt"); + if (!file.open(QIODevice::ReadOnly)) + { + qDebug()<<"File to read the keys could not be opened"; + return SmfPluginErrUserNotLoggedIn; + } + + qDebug()<<"Key file read, going to parse the key values from file"; + + QByteArray arr = file.readAll(); + QList list = arr.split('\n'); + file.close(); + + QString apiKey(list[0]); + QString apiSecret(list[1]); + QString sessionKey(list[2]); + QString sessionSecret(list[3]); + + qDebug()<<"Api Key = "< list = arr.split('\n'); + file.close(); + + QString apiKey(list[0]); + QString apiSecret(list[1]); + QString sessionKey(list[2]); + QString sessionSecret(list[3]); + + qDebug()<<"Api Key = "<initialize(); + } + + +/** + * Method to get the provider information + * @return Instance of SmfProviderBase + */ +SmfProviderBase* FBContactFetcherPlugin::getProviderInfo( ) + { + return m_provider; + } + + +/** + * Method to get the result for a network request. + * @param aOperation The type of operation to be requested + * @param aTransportResult The result of transport operation + * @param aResponse The QByteArray instance containing the network response. + * The plugins should delete this instance once they have read the + * data from it. + * @param aResult [out] An output parameter to the plugin manager.If the + * return value is SmfSendRequestAgain, QVariant will be of type + * SmfPluginRequestData. + * For SmfGalleryPlugin: If last operation was pictures(), aResult will + * be of type QList. If last operation was description(), + * aResult will be of type QString. If last operation was upload() or + * postComment(), aResult will be of type bool. + * @param aRetType [out] SmfPluginRetType + * @param aPageResult [out] The SmfResultPage structure variable + */ +SmfPluginError FBContactFetcherPlugin::responseAvailable( + const SmfRequestTypeID aOperation, + const SmfTransportResult &aTransportResult, + QByteArray *aResponse, + QVariant* aResult, + SmfPluginRetType &aRetType, + SmfResultPage &aPageResult ) + { + Q_UNUSED(aPageResult) + qDebug()<<"Inside FBContactFetcherPlugin::responseAvailable()"; + + SmfPluginError error = SmfPluginErrNetworkError; + + if( !aResponse || (0 == aResponse->size()) ) + { + qDebug()<<"Response is NULL or empty"; + aRetType = SmfRequestError; + return error; + } + + QByteArray response(*aResponse); + delete aResponse; + qDebug()<<"FB response = "<parse(response, &ok); + if (!ok) + { + qDebug()<<"An error occurred during json parsing, error = "<errorString(); + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + else + { + QVariantMap map = result.toMap(); + errStr.append(map["error_msg"].toString()); + } + } + else + { + qDebug()<<"Response is ok, so don't parse"; + response.remove(0, 1); + response.chop(1); + uids.append(response); + } + +#endif + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"UIDs array = "< list; + + QString errStr; + errStr.clear(); + qDebug()<<"For Getting friends details"; + chance = 0; + +#ifdef SMF_XMLPARSING + qDebug()<<"Xml parsing"; + + // For getting contacts from xml response + QXmlStreamReader xml(response); + while (!xml.atEnd()) + { + xml.readNext(); + + if (xml.tokenType() == QXmlStreamReader::StartElement) + { + if (xml.name() == "name") + { + QString message(xml.readElementText()); + + QContactName contactname; + contactname.setFirstName(message); + contactname.setLastName(message); + QVariant nameVar = QVariant::fromValue(contactname); + contact.setValue("Name",nameVar); + } + else if (xml.name() == "uid") + { + QString message(xml.readElementText()); + QContactGuid guid; + guid.setGuid(message); + QVariant guidVar = QVariant::fromValue(guid); + contact.setValue("Guid", guidVar); + } + else if (xml.name() == "pic_square") + { + QUrl url(xml.readElementText()); + qDebug()<<"pic_square = "<().imageUrl().toString(); + list.append(contact); + } + } + } +#else + qDebug()<<"Json parsing"; + + bool ok; + QVariant result = m_util->parse(response, &ok); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + if(response.contains(QByteArray("error_msg"))) + { + QVariantMap map = result.toMap(); + errStr.append(map["error_msg"].toString()); + } + else + { + QList list1 = result.toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfContact contact; + QVariantMap map2 = iter.next().toMap(); + qDebug()<<"name = "< FBContactProviderBase::supportedInterfaces( ) const + { + return m_supportedInterfaces; + } + +/** +* Method to get the list of languages supported by this service provider +* @return a QStringList of languages supported by this service +* provider in 2 letter ISO 639-1 format. +*/ +QStringList FBContactProviderBase::supportedLanguages( ) const + { + return m_supportedLangs; + } + +/** + * Method to get the Plugin specific ID + * @return The Plugin specific ID + */ +QString FBContactProviderBase::pluginId( ) const + { + return m_pluginId; + } + + +/** + * Method to get the ID of the authentication application + * for this service + * @param aProgram The authentication application name + * @param aArguments List of arguments required for authentication app + * @param aMode Strting mode for authentication application + * @return The ID of the authentication application + */ +QString FBContactProviderBase::authenticationApp( QString &aProgram, + QStringList & aArguments, + QIODevice::OpenModeFlag aMode ) const + { + Q_UNUSED(aProgram) + Q_UNUSED(aArguments) + Q_UNUSED(aMode) + return m_authAppId; + } + + +/** + * Method to get the unique registration ID provided by the + * Smf for authorised plugins + * @return The unique registration ID/token provided by the Smf for + * authorised plugins + */ +QString FBContactProviderBase::smfRegistrationId( ) const + { + return m_smfRegToken; + } + + +/** + * Method that initializes this class. This method should be called + * from the initialize() method of the FBContactFetcherPlugin class + */ +void FBContactProviderBase::initialize() + { + m_serviceName = "Facebook"; + m_description = "Facebook contact fetcher plugin description"; + m_serviceUrl = QUrl(QString("http://api.facebook.com")); + m_pluginId = "fbcontactfetcherplugin.qtplugin"; + m_authAppId = "Facebook AuthAppId"; + m_smfRegToken = "Facebook RegToken"; + m_supportedInterfaces.append("org.symbian.smf.plugin.contact.fetcher/v0.2"); + } + + +/* + * Export Macro + * plugin name : fbcontactfetcherplugin + * plugin class : FBContactFetcherPlugin + */ +Q_EXPORT_PLUGIN2( fbcontactfetcherplugin, FBContactFetcherPlugin ) +