diff -r b78fa4cdbf2b -r 106a4bfcb866 example/flickrgalleryplugin/flickrgalleryplugin.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/flickrgalleryplugin/flickrgalleryplugin.cpp Thu Aug 05 16:46:37 2010 +0530 @@ -0,0 +1,1396 @@ +/** + * 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: + * Sangeetha Prasad, Nalina Hariharan + * + * Description: + * The Plugin that fetches gallery related items from the logged in user's flickr account + * + */ + +// Include files +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "flickrgalleryplugin.h" + +static int count = 1; +static int chance = 0; // 0 = for pics from album, 1 = pics not in any album +static int listIndex = 0;// For Mutliple Load +QByteArray payload; +QByteArray boundary("---ThIsIsAsAmPleBouNDaRyStrInGFrOmNaliNa---"); + +// Todo:- Macro added for limiting items fetched to recent 5 +// Remove after demo +#define SETLIMITOFFIVEFORSMFDEMO 1 + +/** + * Method to interpret the key sets obtained from credential manager + * @param aApiKey [out] The api key + * @param aApiSecret [out] The api secret + * @param aAuthToken [out] The auth token provided by Flickr + */ +void FlickrGalleryPlugin::fetchKeys( QString &aApiKey, + QString &aApiSecret, + QString &aAuthToken ) + { + qDebug()<<"Inside FlickrGalleryPlugin::fetchKeys()"; + + qDebug()<<"Reg Token = "<m_smfRegToken; + qDebug()<<"Expiry Date as int = "<m_validity.toTime_t(); + + SmfAuthParams keys; + SmfPluginUtil util; + util.getAuthKeys(keys, m_provider->m_smfRegToken, + m_provider->m_validity, m_provider->m_pluginId); + + qDebug()<<"Number of key-value pairs = "<value("Guid").value().guid(); + qDebug()<<"Flickr user's ID for fetching photos = "< &aImages, + const SmfPictureAlbum* aAlbum ) + { + SmfPluginError error = SmfPluginErrUserNotLoggedIn; + qDebug()<<("\r\n multiple Uploaded Pictures.....= "+QString::number(aImages.count(),10)); + Q_UNUSED(aAlbum) + // Get the key sets from SMF Plugin Utility class. + QString apiKey; + QString apiSecret; + QString authToken; + fetchKeys(apiKey, apiSecret, authToken ); + + count =aImages.count(); + + + QByteArray boundary("---ThIsIsAsAmPleBouNDaRyStrInGFrOmSaNgEeTa---"); + QDataStream stream(&payload, QIODevice::WriteOnly); + int bytes = 0; + + // Create the api_key field + QByteArray keyField = constructField("api_key",apiKey,boundary,"");//,apiKey,boundary); + bytes = stream.writeRawData(keyField.data(), keyField.length()); + qDebug()<<("Bytes written for API key field = "+QString::number(bytes, 10)); + + // Create the auth_token field + QByteArray tokenField = constructField ( "auth_token", authToken, boundary,""); + bytes = stream.writeRawData(tokenField.data(), tokenField.length()); + qDebug()<<("Bytes written for Auth token field = "+QString::number(bytes, 10)); + + //Create the API signature string + QString baseString; + baseString.append(apiSecret); + baseString.append("api_key"+apiKey); + baseString.append("auth_token"+authToken); + //baseString.append("formatjson"); //not working this format + baseString = generateSignature(baseString); + + //extract the smfpicture object + SmfPicture aImage=aImages[listIndex]; + + //Create the signature field + QByteArray sigField = constructField ("api_sig", baseString, boundary,"" ); + bytes = stream.writeRawData(sigField.data(), sigField.length()); + + qDebug()<<("QImage String = "+aImage.url().toString()); + + //Create the file header field + QByteArray fileField = constructField ( "photo", "", boundary, aImage.url().toString()); + bytes = stream.writeRawData(fileField.data(), fileField.length()); + + + //QByteArray pic ; + QImage image=aImage.picture(); + + //for checking only + int byte=image.numBytes(); + qDebug()<<("Bytes written for QImage = "+QString::number(byte,10)); + + //For finding the format of Image file (eg. c:\\data\\image.jpg); + QString fileName=aImage.url().toString(); + int last = fileName.lastIndexOf(".",-1); + qDebug()<<("last QImage = "+QString::number(last,10)); + QString Format=fileName.right(fileName.size()-last-1); //-1 for removing "." from .jpg + qDebug()<<("Format of QImage = "+Format); + + + QByteArray pic; + QBuffer buffer(&pic); + buffer.open(QIODevice::WriteOnly); + image.save(&buffer,Format.toLatin1(),-1); // writes image into format given by Format + + qDebug()<<("Number of bytes in the picture = "+QString::number(buffer.size(), 10)); + + // Add the file content to the stream + if (pic.size()) + { + bytes = stream.writeRawData ( pic.data(), pic.length() ); + qDebug()<<("Bytes written for File content field = "+QString::number(bytes, 10)); + } + qDebug()<<("Before Url string is : "+QString(payload)); + + // Create the end field + QByteArray endField; + endField.append ( "\r\n--" ); + endField.append ( boundary ); + endField.append ( "--\r\n\r\n" ); + bytes = stream.writeRawData(endField.data(), endField.length()); + qDebug()<<("Bytes written for end field = "+QString::number(bytes, 10)); + + //Create the url + QUrl url("http://api.flickr.com/services/upload/"); + //url.addQueryItem("format","json"); //Not working + + + //Create the request, set the url + aRequest.iNetworkRequest.setUrl(url); + + aRequest.iNetworkRequest.setRawHeader("Content-Type", "multipart/form-data; boundary="+boundary); + aRequest.iNetworkRequest.setRawHeader("Host", "api.flickr.com"); + aRequest.iRequestType = SmfPictureUpload; + aRequest.iPostData=new QBuffer(&payload); + + + aRequest.iHttpOperationType = QNetworkAccessManager::PostOperation; + error = SmfPluginErrNone; + return error; + } + + /** + * Method to post comment on a picture is available + * @param aRequest [out] The request data to be sent to network + * @param aImage The image on which comment is to be posted + * @param aComment The comment to be posted + * @return SmfPluginError Plugin error if any, else SmfPluginErrNone + */ +SmfPluginError FlickrGalleryPlugin::postComment( SmfPluginRequestData &aRequest, + const SmfPicture &aImage, + const SmfComment &aComment ) + { + qDebug()<<"Inside FlickrGalleryPlugin::postComment()"; + + SmfPluginError error = SmfPluginErrInvalidArguments; + + if( 0 == aImage.id().size() || (0 == aComment.text().size()) ) + { + qDebug()<<"Invalid arguments"; + return error; + } + + // Get the key sets from SMF Plugin Utility class. + QString apiKey; + QString apiSecret; + QString authToken; + fetchKeys(apiKey, apiSecret, authToken ); + + // Create the API signature string + QString baseString; + baseString.append(apiSecret); + baseString.append("api_key"+apiKey); + baseString.append("auth_token"+authToken); + baseString.append("comment_text"+aComment.text()); + baseString.append("formatjson"); + baseString.append("methodflickr.photos.comments.addComment"); + baseString.append("photo_id"+aImage.id()); + + // Create the url + QUrl url("http://api.flickr.com/services/rest/?"); + url.addQueryItem("api_key", apiKey); + url.addQueryItem("auth_token", authToken); + url.addQueryItem("comment_text", aComment.text()); + url.addQueryItem("format","json"); + url.addQueryItem("method", "flickr.photos.comments.addComment"); + url.addQueryItem("photo_id",aImage.id()); + url.addQueryItem("api_sig", generateSignature(baseString)); + + // Create the request, set the url + aRequest.iNetworkRequest.setUrl(url); + aRequest.iRequestType = SmfPicturePostComment; + aRequest.iPostData = NULL; + aRequest.iHttpOperationType = QNetworkAccessManager::GetOperation; + error = SmfPluginErrNone; + + qDebug()<<"Url string is : "<initialize(); + } + +/** + * Method to get the provider information + * @return Instance of SmfProviderBase + */ +SmfProviderBase* FlickrGalleryPlugin::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 albums, aResult will be of + * type QList. If the 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 FlickrGalleryPlugin::responseAvailable( + const SmfRequestTypeID aOperation, + const SmfTransportResult &aTransportResult, + QByteArray *aResponse, + QVariant* aResult, + SmfPluginRetType &aRetType, + SmfResultPage &aPageResult ) + { + + qDebug()<<"Inside FlickrGalleryPlugin::responseAvailable()"; + Q_UNUSED(aPageResult) + SmfPluginError error = SmfPluginErrNetworkError; + + if( !aResponse || (0 == aResponse->size()) ) + { + qDebug()<<"Response is NULL or empty"; + aRetType = SmfRequestError; + return error; + } + + QByteArray response(*aResponse); + delete aResponse; + + QFile respFile("c://data//SmfPluginFlickrResponse.txt"); + if(!respFile.open(QIODevice::WriteOnly)) + { + qDebug()<<"File to write the response could not be opened, so writing to this file"; + qDebug()<<"Flickr response = "< albumList; + QString errStr; + errStr.clear(); + + // Remove the "jsonFlickrApi(" from starting of the response and ")" from its end + response.remove(0, 14); + response.chop(1); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + + if(response.contains(QByteArray("fail"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1 = result["photosets"].toMap(); + + QList list1 = map1["photoset"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfPictureAlbum album; + + QVariantMap map2 = iter.next().toMap(); + + // Set the album's title + QVariantMap map3 = map2["title"].toMap(); + album.setTitle(map3["_content"].toString()); + qDebug()<<"title = "< picList; + QString errStr; + errStr.clear(); + + // Remove the "jsonFlickrApi(" from starting of the response and ")" from its end + response.remove(0, 14); + response.chop(1); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + + if(response.contains(QByteArray("fail"))) + { + errStr.append(result["message"].toString()); + } + else + { + QVariantMap map1; + if(0 == chance) + map1 = result["photoset"].toMap(); + else + map1 = result["photos"].toMap(); + + QList list1 = map1["photo"].toList(); + QListIterator iter(list1); + while(iter.hasNext()) + { + SmfPicture pic; + + QVariantMap map2 = iter.next().toMap(); + + // Set the pic's id + pic.setId(map2["id"].toString()); + + // Set the pic's description + QVariantMap map3 = map2["description"].toMap(); + pic.setDescription(map3["_content"].toString()); + + // Set the pic's title + pic.setTitle(map2["title"].toString()); + qDebug()<<"title = "<setValue(uploaded); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } +#endif + } + else if (SmfPicturePostComment == aOperation) + { + qDebug()<<"Response for adding comment on a photo"; + + bool commentPosted = false; + QString errStr; + errStr.clear(); + + // Remove the "jsonFlickrApi(" from starting of the response and ")" from its end + response.remove(0, 14); + response.chop(1); + + bool ok; + SmfPluginUtil util; + QVariantMap result = util.parse(response, &ok).toMap(); + if (!ok) + { + qDebug()<<"An error occurred during json parsing"; + aRetType = SmfRequestError; + return SmfPluginErrParsingFailed; + } + + if(response.contains(QByteArray("fail"))) + { + errStr.append(result["message"].toString()); + } + else + { + qDebug()<<"Comment posted on the photo"; + commentPosted = true; + } + + if(errStr.size()) + { + qDebug()<<"Response error found = "<setValue(errStr); + } + else + { + qDebug()<<"comment posted ? "<setValue(commentPosted); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + } + else + { + qDebug()<<"Service unsupported, currently only SmfContactRetrievePosts and SmfContactPostDirected!!!"; + aRetType = SmfRequestError; + error = SmfPluginErrServiceNotSupported; + } + } + + else if(SmfTransportOpOperationCanceledError == aTransportResult) + { + qDebug()<<"Operation Cancelled !!!"; + error = SmfPluginErrCancelComplete; + aRetType = SmfRequestComplete; + } + + else + { + qDebug()<<"Transport Error !!!"; + error = SmfPluginErrNetworkError; + aRetType = SmfRequestError; + } + + return error; + + + + + + + + + + + + + + + + + +#if 0 + qDebug()<<("FlickrGalleryPlugin::responseAvailable"); + Q_UNUSED(aPageResult) + SmfPluginError error = SmfPluginErrNetworkError; + + if( !aResponse || (0 == aResponse->size()) ) + { + qDebug()<<("Response is NULL or empty"); + aRetType = SmfRequestError; + return error; + } + + QByteArray response(*aResponse); + delete aResponse; + + //Write the response to a file + QFile file("c:\\data\\flickrresponse.txt"); + qDebug()<<("response data written to c:\\data\\flickrresponse.txt"); + + if (!file.open(QIODevice::Append | QIODevice::Text)); + file.write(response); + file.close(); + + qDebug()<<("Gallery response size = "+QString::number(response.size(), 10)); + + QList list; + + if(SmfTransportOpNoError == aTransportResult) + { + qDebug()<<("No transport error"); + QVariantMap map1; + bool resType=response.startsWith("setValue(result); + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + + } + else if (SmfPictureMultiUpload == aOperation) + { + bool result; + qDebug()<<("SmfPictureUpload"); + QXmlStreamReader xml(response); + while (!xml.atEnd()) + { + xml.readNext(); + if (xml.tokenType() == QXmlStreamReader::StartElement) + { + qDebug()<<("inside tag"); + //If the tag is contact + if (xml.name() == "photoid") + { + qDebug()<<("photoid tag found"); + result=TRUE; + } + else + result=FALSE; + }//end If + }//endWhile; + + aResult->setValue(result); + if (listIndex < count) + { + + listIndex=listIndex+1; + error = SmfPluginErrNone; + aRetType = SmfSendRequestAgain; + + + } + else + { + //Final result sent + count=1; //clear counter + aRetType = SmfRequestComplete; + error = SmfPluginErrNone; + } + + } + else + { + qDebug()<<("Service unsupported!!!"); + aRetType = SmfRequestError; + error = SmfPluginErrServiceNotSupported; + } + }//end if of if(SmfTransportOpNoError == aTransportResult) + + else if(SmfTransportOpOperationCanceledError == aTransportResult) + { + qDebug()<<("Operation Cancelled !!!"); + error = SmfPluginErrCancelComplete; + aRetType = SmfRequestComplete; + } + + else + { + qDebug()<<("Transport Error !!!"); + error = SmfPluginErrNetworkError; + aRetType = SmfRequestError; + } + return error; +#endif + + } + + +/** + * Destructor + */ +FlickrProviderBase::~FlickrProviderBase( ) + { + } + +/** + * Method to get the Localisable name of the service. + * @return The Localisable name of the service. + */ +QString FlickrProviderBase::serviceName( ) const + { + return m_serviceName; + } + +/** + * Method to get the Logo of the service + * @return The Logo of the service + */ +QImage FlickrProviderBase::serviceIcon( ) const + { + return m_serviceIcon; + } + +/** + * Method to get the Readable service description + * @return The Readable service description + */ +QString FlickrProviderBase::description( ) const + { + return m_description; + } + +/** + * Method to get the Website of the service + * @return The Website of the service + */ +QUrl FlickrProviderBase::serviceUrl( ) const + { + return m_serviceUrl; + } + +/** + * Method to get the URL of the Application providing this service + * @return The URL of the Application providing this service + */ +QUrl FlickrProviderBase::applicationUrl( ) const + { + return m_applicationUrl; + } + +/** + * Method to get the Icon of the application + * @return The Icon of the application + */ +QImage FlickrProviderBase::applicationIcon( ) const + { + return m_applicationIcon; + } + +/** +* Method to get the list of interfaces that this provider support +* @return List of supported Interafces +*/ +QList FlickrProviderBase::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 FlickrProviderBase::supportedLanguages( ) const + { + return m_supportedLangs; + } + +/** + * Method to get the Plugin specific ID + * @return The Plugin specific ID + */ +QString FlickrProviderBase::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 FlickrProviderBase::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 FlickrProviderBase::smfRegistrationId( ) const + { + return m_smfRegToken; + } + +void FlickrProviderBase::initialize() + { + m_serviceName = "Flickr"; + m_description = "Flickr gallery plugin description"; + m_serviceUrl = QUrl(QString("http://api.flickr.com")); + m_pluginId = "flickrgalleryplugin.qtplugin"; + m_authAppId = "0xE1D8C7D7"; + m_supportedInterfaces.append("org.symbian.smf.plugin.gallery/v0.2"); + QSettings iSettings; + m_smfRegToken = iSettings.value("CMFlickrRegToken").toString(); + m_validity = iSettings.value("FlckrExpiryTime").toDateTime(); + } + + +/* + * Export Macro + * plugin name : flickrGalleryplugin + * plugin class : FlickrGalleryPlugin + */ +Q_EXPORT_PLUGIN2( flickrgalleryplugin, FlickrGalleryPlugin )