example/LinkedInAuthApp/src/sessionSP.cpp
changeset 26 83d6a149c755
equal deleted inserted replaced
25:a180113055cb 26:83d6a149c755
       
     1 /**
       
     2 * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "{License}"
       
     6 * which accompanies  this distribution, and is available
       
     7 * at the URL "{LicenseUrl}".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Narasimhulu Kavadapu, Sasken Communication Technologies Ltd - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Siddhartha Chandra, Satish Kanteti Sasken Communication Technologies Ltd
       
    14 * Description:
       
    15 * class to maintian session & all credential keys.
       
    16 */
       
    17 
       
    18 #include "sessionSP.h"
       
    19 #include "requestSP.h"
       
    20 #include <qfile.h>
       
    21 #include <QTimer>
       
    22 #include <qdebug.h>
       
    23 
       
    24 #include "keys.h"
       
    25 
       
    26 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    27 // global
       
    28 
       
    29 static const QString kAPIRestURL = "http://api.facebook.com/restserver.php";
       
    30 static const QString kAPIRestSecureURL = "https://api.facebook.com/restserver.php";
       
    31 
       
    32 
       
    33 static const int kMaxBurstRequests = 3;
       
    34 static const int kBurstDuration = 2;
       
    35 
       
    36 static FBSession* sharedSession = NULL;
       
    37 
       
    38 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    39 // Static class functions
       
    40 FBSession* FBSession::session()
       
    41 {
       
    42     return sharedSession;
       
    43 }
       
    44 
       
    45 void FBSession::setSession(FBSession* aSession)
       
    46 {
       
    47     sharedSession = aSession;
       
    48 }
       
    49 
       
    50 
       
    51 FBSession* FBSession::sessionForApplication ( const QString& aAppKey, const QString& aAppSecret, const QString& aSessionProxy)
       
    52 {
       
    53     FBSession* session = new FBSession ( aAppKey, aAppSecret, aSessionProxy );
       
    54     
       
    55     return session;
       
    56 }
       
    57 
       
    58 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    59 // instance public functions
       
    60 FBSession::FBSession( const QString& aAppKey, const QString& aAppSecret, const QString& aGetSessionProxy ) :
       
    61         iApiKey (aAppKey),
       
    62         iApiSecret ( aAppSecret ),
       
    63         iGetSessionProxy ( aGetSessionProxy ),
       
    64         iRequestBurstCount(0),
       
    65         m_Client(new SmfCredMgrClient(this))	
       
    66 {
       
    67     if (!sharedSession)
       
    68     {
       
    69         sharedSession = this;
       
    70     }
       
    71     
       
    72 }
       
    73 
       
    74 FBSession::~FBSession()
       
    75 {
       
    76 	if(m_Client){
       
    77 		delete m_Client;
       
    78 		m_Client = NULL;
       
    79 	}
       
    80 }
       
    81 
       
    82 const QString& FBSession::apiURL() const
       
    83 {
       
    84     return kApiUrl;
       
    85 }
       
    86 
       
    87 const QString& FBSession::apiSecureURL() const
       
    88 {
       
    89     return kAPIRestSecureURL;
       
    90 }
       
    91 
       
    92 bool FBSession::isConnected() const
       
    93 {
       
    94     return iSessionKey.length() > 0 ;
       
    95 }
       
    96 
       
    97 
       
    98 void FBSession::beginSession (const QString& aSessionKey, const QString& aSessionSecret, const QDateTime& aExpires )
       
    99 {
       
   100 	qDebug()<<"Inside FBSession::beginSession()";
       
   101     iSessionKey = aSessionKey;
       
   102     iSessionSecret = aSessionSecret;
       
   103 
       
   104 
       
   105     iExpirationDate = aExpires;
       
   106 
       
   107     save();
       
   108 }
       
   109 
       
   110 bool FBSession::resume()
       
   111 {
       
   112 	qDebug()<<"Inside FBSession::resume()";
       
   113 	QString fbCMRegToken = iSettings.value("FBCMRegToken").toString();
       
   114 	QDateTime fbExpiryTime = iSettings.value("FBExpiryTime").toDateTime();
       
   115 	SmfAuthParams Params;
       
   116 	if(m_Client->AuthDataSet(fbCMRegToken,fbExpiryTime,Params))
       
   117 	{
       
   118 		QByteArray accessToken = Params.value("accessToken");
       
   119 		emit sessionDidLogin( accessToken );
       
   120         return true;
       
   121     }
       
   122     return false;
       
   123 }
       
   124 
       
   125 void FBSession::cancelLogin() {
       
   126 	qDebug()<<"Inside FBSession::cancelLogin()";
       
   127     if (!isConnected()) {
       
   128         emit sessionDidNotLogin();
       
   129     }
       
   130 }
       
   131 
       
   132 void FBSession::logout() {
       
   133 	qDebug()<<"Inside FBSession::logout()";
       
   134 	iExpirationDate = QDateTime();
       
   135 	iSessionKey.clear();
       
   136 	iSessionSecret.clear();
       
   137 	
       
   138 	unsave();
       
   139 	
       
   140 	emit sessionDidLogout();
       
   141 }
       
   142 
       
   143 void FBSession::send (FBRequest* aRequest) {
       
   144 	qDebug()<<"Inside FBSession::send()";
       
   145     performRequest (aRequest, true);
       
   146 }
       
   147 
       
   148 
       
   149 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
   150 // instance private functions
       
   151 void FBSession::save()
       
   152 {
       
   153 	qDebug()<<"Inside FBSession::save()";
       
   154     SmfAuthParams Params;
       
   155     Params.insert("ApiKey",kApiKey.toAscii());
       
   156    // Params.insert("ApiSecret",kApiSecret.toAscii());
       
   157   //  Params.insert("AppId",kAppId.toAscii());
       
   158     Params.insert("SessionKey",iSessionKey.toAscii());
       
   159     Params.insert("SessionSecret",iSessionSecret.toAscii());
       
   160     
       
   161     
       
   162     QList<QUrl> UrlList;
       
   163     UrlList.append(QUrl("http://api.facebook.com"));
       
   164     UrlList.append(QUrl("http://www.facebook.com"));
       
   165     
       
   166     QStringList PluginList;
       
   167     PluginList.append(QString("fbactivityfetcherplugin.qtplugin"));
       
   168     PluginList.append(QString("fbcontactfetcherplugin.qtplugin"));
       
   169     PluginList.append(QString("fbpostproviderplugin.qtplugin"));
       
   170     
       
   171     QString UID("0xEFE2FD23");
       
   172     
       
   173     //Currently Hardcoded with current time bcoz CM is not handling expiry time as '0' value
       
   174     iExpirationDate = QDateTime::currentDateTime();
       
   175     iExpirationDate.addYears(1);
       
   176     
       
   177     QString fbCMRegToken = m_Client->StoreAuthData(Params,iExpirationDate,UrlList,PluginList,UID,true);
       
   178     qDebug()<<"Reg token returned by auth app = "<<fbCMRegToken;
       
   179     
       
   180     iSettings.remove("FBCMRegToken");
       
   181     iSettings.remove("FBExpiryTime");
       
   182     
       
   183     if(fbCMRegToken.size()){
       
   184     	iSettings.setValue("FBCMRegToken", fbCMRegToken);
       
   185     	iSettings.setValue("FBExpiryTime", iExpirationDate);
       
   186     }
       
   187 }
       
   188 
       
   189 void FBSession::unsave()
       
   190 {
       
   191 	qDebug()<<"Inside FBSession::unsave()";
       
   192 	//Delete saved keys from Credential Manager.
       
   193 }
       
   194 
       
   195 void FBSession::startFlushTimer()
       
   196 {
       
   197 	int t = kBurstDuration;
       
   198     QTimer::singleShot( t, this, SLOT(requestTimerReady()));
       
   199 }
       
   200 
       
   201 void FBSession::enqueueRequest(FBRequest* aRequest)
       
   202 {
       
   203     iRequestQueue.append(aRequest);
       
   204     startFlushTimer();
       
   205 }
       
   206 
       
   207 bool FBSession::performRequest(FBRequest* aRequest, bool aEnqueue) {
       
   208     // Stagger requests that happen in short bursts to prevent the server from rejecting
       
   209     // them for making too many requests in a short time
       
   210 
       
   211     int seconds = iLastRequestTime.secsTo( QDateTime::currentDateTime() );
       
   212 	bool burst = seconds && (seconds < kBurstDuration);
       
   213 
       
   214 	if (burst && (iRequestBurstCount > kMaxBurstRequests))
       
   215     {
       
   216         if (aEnqueue)
       
   217         {
       
   218             enqueueRequest(aRequest);
       
   219         }
       
   220         return false;
       
   221     }
       
   222     else
       
   223     {
       
   224         aRequest->connect();
       
   225 		if (burst) {
       
   226 			iRequestBurstCount++;
       
   227 		} else {
       
   228             iRequestBurstCount = 1;
       
   229             iLastRequestTime = aRequest->timeStamp();
       
   230         }
       
   231     }
       
   232     return true;
       
   233 }
       
   234 
       
   235 void FBSession::flushRequestQueue()
       
   236 {
       
   237     while ( iRequestQueue.count() ) {
       
   238       FBRequest* request = iRequestQueue.at(0);
       
   239       if (performRequest(request, false)) {
       
   240           iRequestQueue.removeAt(0);
       
   241       } else {
       
   242         startFlushTimer();
       
   243         break;
       
   244       }
       
   245     }
       
   246 }
       
   247 
       
   248 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
   249 // instance provate slots
       
   250 void FBSession::requestTimerReady()
       
   251 {
       
   252     flushRequestQueue();
       
   253 }