26
|
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, 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 kBase;
|
|
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("ConsumerKey",kConsumerKey.toAscii());
|
|
157 |
Params.insert("ConsumerSecret",kConsumerSecret.toAscii());
|
|
158 |
Params.insert("oauth_Token",stroauth_Token.toAscii());
|
|
159 |
Params.insert("oauth_TokenSecret",stroauth_TokenSecret.toAscii());
|
|
160 |
Params.insert("oauth_verifier",stroauth_verifier.toAscii());
|
|
161 |
|
|
162 |
QList<QUrl> UrlList;
|
|
163 |
UrlList.append(QUrl("https://api.twitter.com/oauth/"));
|
|
164 |
|
|
165 |
QStringList PluginList;
|
|
166 |
PluginList.append(QString("twitter.qtplugin"));
|
|
167 |
|
|
168 |
QString UID("0xEFE2EF23");
|
|
169 |
|
|
170 |
//Currently Hardcoded with current time bcoz CM is not handling expiry time as '0' value
|
|
171 |
iExpirationDate = QDateTime::currentDateTime();
|
|
172 |
iExpirationDate.addYears(1);
|
|
173 |
|
|
174 |
QString twCMRegToken = m_Client->StoreAuthData(Params,iExpirationDate,UrlList,PluginList,UID,true);
|
|
175 |
qDebug()<<"Reg token returned by auth app = "<<twCMRegToken;
|
|
176 |
|
|
177 |
iSettings.remove("twCMRegToken");
|
|
178 |
iSettings.remove("twExpiryTime");
|
|
179 |
|
|
180 |
if(twCMRegToken.size()){
|
|
181 |
iSettings.setValue("twCMRegToken", twCMRegToken);
|
|
182 |
iSettings.setValue("twExpiryTime", iExpirationDate);
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
void FBSession::unsave()
|
|
187 |
{
|
|
188 |
qDebug()<<"Inside FBSession::unsave()";
|
|
189 |
//Delete saved keys from Credential Manager.
|
|
190 |
}
|
|
191 |
|
|
192 |
void FBSession::startFlushTimer()
|
|
193 |
{
|
|
194 |
int t = kBurstDuration;
|
|
195 |
QTimer::singleShot( t, this, SLOT(requestTimerReady()));
|
|
196 |
}
|
|
197 |
|
|
198 |
void FBSession::enqueueRequest(FBRequest* aRequest)
|
|
199 |
{
|
|
200 |
iRequestQueue.append(aRequest);
|
|
201 |
startFlushTimer();
|
|
202 |
}
|
|
203 |
|
|
204 |
bool FBSession::performRequest(FBRequest* aRequest, bool aEnqueue) {
|
|
205 |
// Stagger requests that happen in short bursts to prevent the server from rejecting
|
|
206 |
// them for making too many requests in a short time
|
|
207 |
|
|
208 |
int seconds = iLastRequestTime.secsTo( QDateTime::currentDateTime() );
|
|
209 |
bool burst = seconds && (seconds < kBurstDuration);
|
|
210 |
|
|
211 |
if (burst && (iRequestBurstCount > kMaxBurstRequests))
|
|
212 |
{
|
|
213 |
if (aEnqueue)
|
|
214 |
{
|
|
215 |
enqueueRequest(aRequest);
|
|
216 |
}
|
|
217 |
return false;
|
|
218 |
}
|
|
219 |
else
|
|
220 |
{
|
|
221 |
aRequest->connect();
|
|
222 |
if (burst) {
|
|
223 |
iRequestBurstCount++;
|
|
224 |
} else {
|
|
225 |
iRequestBurstCount = 1;
|
|
226 |
iLastRequestTime = aRequest->timeStamp();
|
|
227 |
}
|
|
228 |
}
|
|
229 |
return true;
|
|
230 |
}
|
|
231 |
|
|
232 |
void FBSession::flushRequestQueue()
|
|
233 |
{
|
|
234 |
while ( iRequestQueue.count() ) {
|
|
235 |
FBRequest* request = iRequestQueue.at(0);
|
|
236 |
if (performRequest(request, false)) {
|
|
237 |
iRequestQueue.removeAt(0);
|
|
238 |
} else {
|
|
239 |
startFlushTimer();
|
|
240 |
break;
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
246 |
// instance provate slots
|
|
247 |
void FBSession::requestTimerReady()
|
|
248 |
{
|
|
249 |
flushRequestQueue();
|
|
250 |
}
|
|
251 |
void FBSession::send_req (QString uri,FBRequest* aRequest)
|
|
252 |
{
|
|
253 |
aRequest->connect_req(uri);
|
|
254 |
}
|