example/fbpostproviderplugin/fbpostproviderplugin.cpp
changeset 6 c39a6cfd1fb9
equal deleted inserted replaced
5:edb9dc8273d9 6:c39a6cfd1fb9
       
     1 
       
     2 // Include files
       
     3 #include <QtPlugin>
       
     4 #include <QCryptographicHash>
       
     5 #include <QTextFormat>
       
     6 #include <QTextStream>
       
     7 #include <QFile>
       
     8 #include <QNetworkReply>
       
     9 #include <QXmlStreamReader>
       
    10 
       
    11 #include "fbpostproviderplugin.h"
       
    12 
       
    13 // Added for flickr testing - start - put your registered app's  keys here
       
    14 static const QString apiKey = "";
       
    15 static const QString apiSecret = "";
       
    16 static const QString sessionKey = "";
       
    17 static const QString sessionSecret = "";
       
    18 
       
    19 
       
    20 /**
       
    21  * Method called by plugins to generate a signature string from a base string
       
    22  * @param aBaseString The base string
       
    23  * @return The md5 hash of the base string
       
    24  */
       
    25 QString FBPostProviderPlugin::generateSignature(const QString aBaseString)
       
    26 	{
       
    27 	writeLog("FBPostProviderPlugin::generateSignature");
       
    28 
       
    29 	// Create md5 hash of the signature string
       
    30     QByteArray byteArray;
       
    31     byteArray.insert(0, aBaseString.toAscii());
       
    32 
       
    33     QByteArray md5Hash = QCryptographicHash::hash(byteArray,QCryptographicHash::Md5 ).toHex();
       
    34     QString returnString (md5Hash);
       
    35     return returnString;
       
    36 	}
       
    37 
       
    38 
       
    39 /**
       
    40  * Method called by plugins for logging
       
    41  * @param log string to be logged
       
    42  */
       
    43 void FBPostProviderPlugin::writeLog(QString log) const
       
    44 	{
       
    45 	QFile file("c:\\data\\PluginLogs.txt");
       
    46     if (!file.open(QIODevice::Append | QIODevice::Text))
       
    47 	         ;
       
    48     QTextStream out(&file);
       
    49     out << log << "\n";
       
    50     file.close();
       
    51 	}
       
    52 
       
    53 /**
       
    54  * Destructor
       
    55  */
       
    56 FBPostProviderPlugin::~FBPostProviderPlugin( )
       
    57 	{
       
    58 	if(m_provider)
       
    59 		delete m_provider;
       
    60 	}
       
    61 
       
    62 /**
       
    63  * Method that returns maximum no of chars (unicode) that service
       
    64  * provider can post without truncation. Negative value means
       
    65  * no limit
       
    66  * @return Max characters that can be posted without truncation
       
    67  */
       
    68 qint32 FBPostProviderPlugin::maxCharsInPost( ) const
       
    69 		{
       
    70 	qint32 maxCharsInPost = 256;
       
    71 	return maxCharsInPost;
       
    72 		}
       
    73 /**
       
    74  * Method that returns maximum no of items that can be returned
       
    75  * in a single query to getPosts. Negative value means feature
       
    76  * not supported.
       
    77  * @return Max items that can be returned in a single query
       
    78  */
       
    79 qint32 FBPostProviderPlugin::maxItems( ) const
       
    80 		{
       
    81 	qint32 maxItems = 10;
       
    82 	return maxItems;
       
    83 		}
       
    84 
       
    85 /**
       
    86  * <Method that returns all the formatting of posts that this
       
    87  * service provider supports. May return 0 items to mean
       
    88  * only QString is supported.
       
    89  * @return Supported formats of posts
       
    90  */
       
    91 QVector<QTextFormat> FBPostProviderPlugin::supportedFormats ( ) const
       
    92 		{
       
    93 	QVector<QTextFormat> data;
       
    94 	return data;
       
    95 		}
       
    96 
       
    97 /**
       
    98  * Method that returns whether this SP supports Appearence
       
    99  * @return Returns true if Appearance is supported, else false.
       
   100  * @see SmfAppearenceInfo
       
   101  */
       
   102 bool FBPostProviderPlugin::supportsAppearence ( ) const
       
   103 		{
       
   104 	return false;
       
   105 		}
       
   106 
       
   107 /**
       
   108  * Method to get the latest posts
       
   109  * @param aRequest [out] The request data to be sent to network
       
   110  * @param aUser The user's contact in this SP, omit for self contact
       
   111  * @param aPageNum The page to be extracted
       
   112  * @param aItemsPerPage Number of items per page
       
   113  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   114  */
       
   115 SmfPluginError FBPostProviderPlugin::retrieve( SmfPluginRequestData &aRequest,
       
   116 		const SmfContact *aUser,
       
   117 		const int aPageNum ,
       
   118 		const int aItemsPerPage  )
       
   119 	{
       
   120 	Q_UNUSED(aUser)
       
   121 	writeLog("Inside FBPostProviderPlugin::retrieve");
       
   122 
       
   123 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   124 
       
   125 	//// Currently considering for self contatc , ie, omitting aUser
       
   126 	// invalid arguments
       
   127 	if( aPageNum < 0 || aItemsPerPage < 0 )
       
   128 		return error;
       
   129 	else
       
   130 		{
       
   131 		// Get the current date and time and convert it to sec as a string
       
   132 		QString call_id = QString::number(QDateTime::currentDateTime().toTime_t(), 10);
       
   133 
       
   134 		// Create the API signature string
       
   135 		QString baseString;
       
   136 		baseString.append("api_key="+apiKey);
       
   137 		baseString.append("call_id="+call_id);
       
   138 		baseString.append("format=XML");
       
   139 		baseString.append("method=stream.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 		url.addQueryItem("format", "XML");
       
   150 		url.addQueryItem("method", "stream.get");
       
   151 		url.addQueryItem("session_key", sessionKey);
       
   152 		url.addQueryItem("ss", "1");
       
   153 		url.addQueryItem("v", "1.0");
       
   154 		url.addQueryItem("sig", generateSignature(baseString));
       
   155 
       
   156 		// Create the request, set the url
       
   157 		aRequest.iNetworkRequest.setUrl(url);
       
   158 		aRequest.iRequestType = SmfContactRetrievePosts;
       
   159 		aRequest.iPostData = NULL;
       
   160 		aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation;
       
   161 		error = SmfPluginErrNone;
       
   162 		}
       
   163 	writeLog("Url string is : "+aRequest.iNetworkRequest.url().toString());
       
   164 	return error;
       
   165 	}
       
   166 
       
   167 /**
       
   168  * Method to update a post to own area.
       
   169  * @param aRequest [out] The request data to be sent to network
       
   170  * @param aPostData The post data to be posted
       
   171  * @param aLocation The location
       
   172  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   173  */
       
   174 SmfPluginError FBPostProviderPlugin::post( SmfPluginRequestData &aRequest,
       
   175 		const SmfPost &aPostData,
       
   176 		const SmfLocation &aLocation )
       
   177 	{
       
   178 	Q_UNUSED(aRequest)
       
   179 Q_UNUSED(aPostData)
       
   180 Q_UNUSED(aLocation)
       
   181 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   182 	return error;
       
   183 	}
       
   184 
       
   185 /**
       
   186  * Method to update the last post to own area with new data
       
   187  * @param aRequest [out] The request data to be sent to network
       
   188  * @param aPostData The edited/new data to be posted
       
   189  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   190  */
       
   191 SmfPluginError FBPostProviderPlugin::updatePost( SmfPluginRequestData &aRequest,
       
   192 		const SmfPost &aPostData )
       
   193 	{
       
   194 	Q_UNUSED(aRequest)
       
   195 Q_UNUSED(aPostData)
       
   196 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   197 	return error;
       
   198 	}
       
   199 
       
   200 /**
       
   201  * Method to update a post to a particular contact
       
   202  * @param aRequest [out] The request data to be sent to network
       
   203  * @param aPostData The post data to be posted
       
   204  * @param aContact The contact where the data has to be posted
       
   205  * @param aLocation The location
       
   206  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   207  */
       
   208 SmfPluginError FBPostProviderPlugin::postDirected( SmfPluginRequestData &aRequest,
       
   209 		const SmfPost &aPostData,
       
   210 		const SmfContact &aContact,
       
   211 		const SmfLocation *aLocation  )
       
   212 	{
       
   213 	Q_UNUSED(aRequest)
       
   214 Q_UNUSED(aPostData)
       
   215 Q_UNUSED(aContact)
       
   216 Q_UNUSED(aLocation)
       
   217 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   218 	return error;
       
   219 	}
       
   220 
       
   221 
       
   222 /**
       
   223  * Method to post a comment on a post.
       
   224  * @param aRequest [out] The request data to be sent to network
       
   225  * @param aTarget Post on which comment has to be posted
       
   226  * @param aComment comment to be posted
       
   227  * @param aLocation location data
       
   228  */
       
   229 SmfPluginError FBPostProviderPlugin::commentOnAPost(SmfPluginRequestData &aRequest,
       
   230 		const SmfPost &aTarget,
       
   231 		const SmfPost &aComment,
       
   232 		const SmfLocation *aLocation )
       
   233 	{
       
   234 	Q_UNUSED(aRequest)
       
   235 Q_UNUSED(aTarget)
       
   236 Q_UNUSED(aComment)
       
   237 Q_UNUSED(aLocation)
       
   238 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   239 	return error;
       
   240 	}
       
   241 
       
   242 /**
       
   243  * Method to update the presence information of the user
       
   244  * @param aRequest [out] The request data to be sent to network
       
   245  * @param aAppearence The appearence information
       
   246  * @param aStatus The status string
       
   247  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   248  */
       
   249 SmfPluginError FBPostProviderPlugin::postAppearence( SmfPluginRequestData &aRequest,
       
   250 		const SmfPresenceInfo &aAppearence,
       
   251 		const QString &aStatus )
       
   252 	{
       
   253 	Q_UNUSED(aRequest)
       
   254 Q_UNUSED(aAppearence)
       
   255 Q_UNUSED(aStatus)
       
   256 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   257 	return error;
       
   258 	}
       
   259 
       
   260 /**
       
   261  * Share a contact's post to user's friends and followers
       
   262  * (e.g. retweet in twitter, share on facebook)
       
   263  * @param aRequest [out] The request data to be sent to network
       
   264  * @param aPostData data to be posted
       
   265  * @param aContact contact to which the post belonged
       
   266  * @param aEdited whether user changed items within the post
       
   267  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   268  */
       
   269 SmfPluginError FBPostProviderPlugin::sharePost( SmfPluginRequestData &aRequest,
       
   270 		const SmfPost &aPostData,
       
   271 		const SmfContact &aContact,
       
   272 		const bool &aEdited)
       
   273 	{
       
   274 	Q_UNUSED(aRequest)
       
   275 Q_UNUSED(aPostData)
       
   276 Q_UNUSED(aContact)
       
   277 Q_UNUSED(aEdited)
       
   278 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   279 	return error;
       
   280 	}
       
   281 
       
   282 /**
       
   283  * Customised method for SmfPostProviderPlugin interface
       
   284  * @param aRequest [out] The request data to be sent to network
       
   285  * @param aOperation The operation type (should be known between
       
   286  * the client interface and the plugin)
       
   287  * @param aData The data required to form the request (The type
       
   288  * of data should be known between client and the plugin)
       
   289  * @return SmfPluginError Plugin error if any, else SmfPluginErrNone
       
   290  */
       
   291 SmfPluginError FBPostProviderPlugin::customRequest( SmfPluginRequestData &aRequest,
       
   292 		const int &aOperation, QByteArray *aData )
       
   293 	{
       
   294 	Q_UNUSED(aRequest)
       
   295 Q_UNUSED(aOperation)
       
   296 Q_UNUSED(aData)
       
   297 	SmfPluginError error = SmfPluginErrInvalidRequest;
       
   298 	return error;
       
   299 	}
       
   300 
       
   301 
       
   302 /**
       
   303  * The first method to be called in the plugin that implements this interface.
       
   304  * If this method is not called, plugin may not behave as expected.
       
   305  * Plugins are expected to save the aUtil handle and use and when required.
       
   306  * @param aUtil The instance of SmfPluginUtil
       
   307  */
       
   308 void FBPostProviderPlugin::initialize( SmfPluginUtil *aUtil )
       
   309 	{
       
   310 	// Save the SmfPluginUtil handle
       
   311 	m_util = aUtil;
       
   312 
       
   313 	// Create an instance of FlickrProviderBase
       
   314 	m_provider = new FBProviderBase;
       
   315 	m_provider->initialize();
       
   316 	}
       
   317 
       
   318 /**
       
   319  * Method to get the provider information
       
   320  * @return Instance of SmfProviderBase
       
   321  */
       
   322 SmfProviderBase* FBPostProviderPlugin::getProviderInfo( )
       
   323 	{
       
   324 	return m_provider;
       
   325 	}
       
   326 
       
   327 /**
       
   328  * Method to get the result for a network request.
       
   329  * @param aTransportResult The result of transport operation
       
   330  * @param aResponse The QByteArray instance containing the network response.
       
   331  * The plugins should delete this instance once they have read the
       
   332  * data from it.
       
   333  * @param aResult [out] An output parameter to the plugin manager.If the
       
   334  * return value is SmfSendRequestAgain, QVariant will be of type
       
   335  * SmfPluginRequestData.
       
   336  * For SmfPostProviderPlugin: If last operation was retrieve(), aResult will be
       
   337  * of type QList<SmfPost>. If last operation was post() or updatePost() or
       
   338  * postDirected() or commentOnAPost() or postAppearence() or sharePost(),
       
   339  * aResult will be of type bool
       
   340  * @param aRetType [out] SmfPluginRetType
       
   341  * @param aPageResult [out] The SmfResultPage structure variable
       
   342  */
       
   343 SmfPluginError FBPostProviderPlugin::responseAvailable(
       
   344 		const SmfTransportResult &aTransportResult,
       
   345 		QByteArray *aResponse,
       
   346 		QVariant* aResult,
       
   347 		SmfPluginRetType &aRetType,
       
   348 		SmfResultPage &aPageResult )
       
   349 	{
       
   350 	writeLog("FBPostProviderPlugin::::responseAvailable");
       
   351 	Q_UNUSED(aPageResult)
       
   352 	//This API is slightly changed by Manasij
       
   353 	SmfPluginError error;
       
   354 	if(SmfTransportOpNoError == aTransportResult)
       
   355 		{
       
   356 		writeLog("No transport error");
       
   357 
       
   358 		// Write the response to a file
       
   359 		QFile file("c:\\data\\fbresponse.txt");
       
   360 		writeLog("response data written to c:\\data\\fbresponse.txt");
       
   361 		if (!file.open(QIODevice::Append | QIODevice::Text))
       
   362 				 ;
       
   363 		file.write(aResponse->data());
       
   364 		file.close();
       
   365 
       
   366 		QList<SmfPost> list;
       
   367 
       
   368 		// For getting contacts
       
   369 		QXmlStreamReader xml(aResponse->data());
       
   370 		while (!xml.atEnd())
       
   371 			{
       
   372 			xml.readNext();
       
   373 			if (xml.tokenType() == QXmlStreamReader::StartElement)
       
   374 				{
       
   375 				// If the tag is contact
       
   376 				if (xml.name() == "message")
       
   377 					{
       
   378 					writeLog("message tag found");
       
   379 
       
   380 					SmfPost post;
       
   381 					QString descStr(xml.readElementText());
       
   382 					post.setDescription(descStr);
       
   383 
       
   384                     list.append(post);
       
   385 					}
       
   386 				}
       
   387 			}
       
   388 
       
   389 		aResult->setValue(list);
       
   390 		aRetType = SmfRequestComplete;
       
   391 		error = SmfPluginErrNone;
       
   392 		}
       
   393 
       
   394 	else
       
   395 		{
       
   396 		error = SmfPluginErrInvalidRequest;
       
   397 		aRetType = SmfRequestError;
       
   398 		}
       
   399 	delete aResponse;
       
   400 	return error;
       
   401 	}
       
   402 
       
   403 
       
   404 /**
       
   405  * Destructor
       
   406  */
       
   407 FBProviderBase::~FBProviderBase( )
       
   408 	{
       
   409 	}
       
   410 
       
   411 /**
       
   412  * Method to get the Localisable name of the service.
       
   413  * @return The Localisable name of the service.
       
   414  */
       
   415 QString FBProviderBase::serviceName( ) const
       
   416 	{
       
   417 	return m_serviceName;
       
   418 	}
       
   419 
       
   420 /**
       
   421  * Method to get the Logo of the service
       
   422  * @return The Logo of the service
       
   423  */
       
   424 QImage FBProviderBase::serviceIcon( ) const
       
   425 	{
       
   426 	return m_serviceIcon;
       
   427 	}
       
   428 
       
   429 /**
       
   430  * Method to get the Readable service description
       
   431  * @return The Readable service description
       
   432  */
       
   433 QString FBProviderBase::description( ) const
       
   434 	{
       
   435 	return m_description;
       
   436 	}
       
   437 
       
   438 /**
       
   439  * Method to get the Website of the service
       
   440  * @return The Website of the service
       
   441  */
       
   442 QUrl FBProviderBase::serviceUrl( ) const
       
   443 	{
       
   444 	return m_serviceUrl;
       
   445 	}
       
   446 
       
   447 /**
       
   448  * Method to get the URL of the Application providing this service
       
   449  * @return The URL of the Application providing this service
       
   450  */
       
   451 QUrl FBProviderBase::applicationUrl( ) const
       
   452 	{
       
   453 	return m_applicationUrl;
       
   454 	}
       
   455 
       
   456 /**
       
   457  * Method to get the Icon of the application
       
   458  * @return The Icon of the application
       
   459  */
       
   460 QImage FBProviderBase::applicationIcon( ) const
       
   461 	{
       
   462 	return m_applicationIcon;
       
   463 	}
       
   464 
       
   465 /**
       
   466  * Method to get the Plugin specific ID
       
   467  * @return The Plugin specific ID
       
   468  */
       
   469 QString FBProviderBase::pluginId( ) const
       
   470 	{
       
   471 	return m_pluginId;
       
   472 	}
       
   473 
       
   474 /**
       
   475  * Method to get the ID of the authentication application
       
   476  * for this service
       
   477  * @param aProgram The authentication application name
       
   478  * @param aArguments List of arguments required for authentication app
       
   479  * @param aMode Strting mode for authentication application
       
   480  * @return The ID of the authentication application
       
   481  */
       
   482 QString FBProviderBase::authenticationApp( QString &aProgram,
       
   483 		QStringList & aArguments,
       
   484 		QIODevice::OpenModeFlag aMode ) const
       
   485 	{
       
   486 	Q_UNUSED(aProgram)
       
   487 Q_UNUSED(aArguments)
       
   488 Q_UNUSED(aMode)
       
   489 	return m_authAppId;
       
   490 	}
       
   491 
       
   492 /**
       
   493  * Method to get the unique registration ID provided by the
       
   494  * Smf for authorised plugins
       
   495  * @return The unique registration ID/token provided by the Smf for
       
   496  * authorised plugins
       
   497  */
       
   498 QString FBProviderBase::smfRegistrationId( ) const
       
   499 	{
       
   500 	return m_smfRegToken;
       
   501 	}
       
   502 
       
   503 void FBProviderBase::initialize()
       
   504 	{
       
   505 	m_serviceName = "Facebook";
       
   506 	m_description = "Facebook plugin description";
       
   507 	m_serviceUrl = QUrl(QString("http://api.facebook.com"));
       
   508 	m_pluginId = "fbpostproviderplugin.qtplugin";
       
   509 	m_authAppId = "Facebook AuthAppId";
       
   510 	m_smfRegToken = "Facebook RegToken";
       
   511 	}
       
   512 
       
   513 
       
   514 /*
       
   515  * Export Macro
       
   516  * plugin name : fbpostproviderplugin
       
   517  * plugin class : FBPostProviderPlugin
       
   518  */
       
   519 Q_EXPORT_PLUGIN2( fbpostproviderplugin, FBPostProviderPlugin )
       
   520