|
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 * |
|
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 // global |
|
25 |
|
26 static const QString kAPIRestURL = "http://api.facebook.com/restserver.php"; |
|
27 static const QString kAPIRestSecureURL = "https://api.facebook.com/restserver.php"; |
|
28 |
|
29 static const int kMaxBurstRequests = 3; |
|
30 static const int kBurstDuration = 2; |
|
31 |
|
32 static FBSession* sharedSession = NULL; |
|
33 |
|
34 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
35 // Static class functions |
|
36 FBSession* FBSession::session() |
|
37 { |
|
38 return sharedSession; |
|
39 } |
|
40 |
|
41 void FBSession::setSession(FBSession* aSession) |
|
42 { |
|
43 sharedSession = aSession; |
|
44 } |
|
45 |
|
46 |
|
47 FBSession* FBSession::sessionForApplication ( const QString& aAppKey, const QString& aAppSecret, const QString& aSessionProxy) |
|
48 { |
|
49 FBSession* session = new FBSession ( aAppKey, aAppSecret, aSessionProxy ); |
|
50 |
|
51 return session; |
|
52 } |
|
53 |
|
54 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
55 // instance public functions |
|
56 FBSession::FBSession( const QString& aAppKey, const QString& aAppSecret, const QString& aGetSessionProxy ) : |
|
57 iApiKey (aAppKey), |
|
58 iApiSecret ( aAppSecret ), |
|
59 iGetSessionProxy ( aGetSessionProxy ), |
|
60 iRequestBurstCount(0), |
|
61 m_Client(new SmfCredMgrClient(this)) |
|
62 { |
|
63 if (!sharedSession) |
|
64 { |
|
65 sharedSession = this; |
|
66 } |
|
67 |
|
68 } |
|
69 |
|
70 FBSession::~FBSession() |
|
71 { |
|
72 if(m_Client){ |
|
73 delete m_Client; |
|
74 m_Client = NULL; |
|
75 } |
|
76 } |
|
77 |
|
78 const QString& FBSession::apiURL() const |
|
79 { |
|
80 return kAPIRestURL; |
|
81 } |
|
82 |
|
83 const QString& FBSession::apiSecureURL() const |
|
84 { |
|
85 return kAPIRestSecureURL; |
|
86 } |
|
87 |
|
88 bool FBSession::isConnected() const |
|
89 { |
|
90 return iSessionKey.length() > 0 ; |
|
91 } |
|
92 |
|
93 |
|
94 void FBSession::beginSession (const QString& aSessionKey, const QString& aSessionSecret, const QDateTime& aExpires ) |
|
95 { |
|
96 iSessionKey = aSessionKey; |
|
97 iSessionSecret = aSessionSecret; |
|
98 |
|
99 |
|
100 iExpirationDate = aExpires; |
|
101 |
|
102 save(); |
|
103 } |
|
104 |
|
105 bool FBSession::resume() |
|
106 { |
|
107 QString CMRegToken = iSettings.value("CMRegToken").toString(); |
|
108 QDateTime ExpiryTime = iSettings.value("ExpiryTime").toDateTime(); |
|
109 SmfAuthParams Params; |
|
110 if(m_Client->AuthDataSet(CMRegToken,ExpiryTime,Params)) |
|
111 { |
|
112 QByteArray accessToken = Params.value("accessToken"); |
|
113 emit sessionDidLogin( accessToken ); |
|
114 return true; |
|
115 } |
|
116 return false; |
|
117 } |
|
118 |
|
119 void FBSession::cancelLogin() { |
|
120 if (!isConnected()) { |
|
121 emit sessionDidNotLogin(); |
|
122 } |
|
123 } |
|
124 |
|
125 void FBSession::logout() { |
|
126 |
|
127 iExpirationDate = QDateTime(); |
|
128 iSessionKey.clear(); |
|
129 iSessionSecret.clear(); |
|
130 |
|
131 unsave(); |
|
132 |
|
133 emit sessionDidLogout(); |
|
134 } |
|
135 |
|
136 void FBSession::send (FBRequest* aRequest) { |
|
137 performRequest (aRequest, true); |
|
138 } |
|
139 |
|
140 |
|
141 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
142 // instance private functions |
|
143 void FBSession::save() |
|
144 { |
|
145 |
|
146 SmfAuthParams Params; |
|
147 Params.insert("accessToken",iSessionKey.toAscii()); |
|
148 |
|
149 QList<QUrl> UrlList; |
|
150 UrlList.append(QUrl("http://www.facebook.com")); |
|
151 |
|
152 QStringList PluginList; |
|
153 PluginList.append(QString("facebook")); |
|
154 |
|
155 QString UID("0xEFE2FD23"); |
|
156 |
|
157 //Currently Hardcoded with current time bcoz CM is not handling expiry time as '0' value |
|
158 iExpirationDate = QDateTime::currentDateTime(); |
|
159 iExpirationDate.addYears(1); |
|
160 |
|
161 QString CMRegToken = m_Client->StoreAuthData(Params,iExpirationDate,UrlList,PluginList,UID,true); |
|
162 |
|
163 if(CMRegToken.size()){ |
|
164 iSettings.setValue("CMRegToken", CMRegToken); |
|
165 iSettings.setValue("ExpiryTime", iExpirationDate); |
|
166 } |
|
167 } |
|
168 |
|
169 void FBSession::unsave() |
|
170 { |
|
171 //Delete saved keys from Credential Manager. |
|
172 } |
|
173 |
|
174 void FBSession::startFlushTimer() |
|
175 { |
|
176 int t = kBurstDuration; |
|
177 QTimer::singleShot( t, this, SLOT(requestTimerReady())); |
|
178 } |
|
179 |
|
180 void FBSession::enqueueRequest(FBRequest* aRequest) |
|
181 { |
|
182 iRequestQueue.append(aRequest); |
|
183 startFlushTimer(); |
|
184 } |
|
185 |
|
186 bool FBSession::performRequest(FBRequest* aRequest, bool aEnqueue) { |
|
187 // Stagger requests that happen in short bursts to prevent the server from rejecting |
|
188 // them for making too many requests in a short time |
|
189 |
|
190 int seconds = iLastRequestTime.secsTo( QDateTime::currentDateTime() ); |
|
191 bool burst = seconds && (seconds < kBurstDuration); |
|
192 |
|
193 if (burst && (iRequestBurstCount > kMaxBurstRequests)) |
|
194 { |
|
195 if (aEnqueue) |
|
196 { |
|
197 enqueueRequest(aRequest); |
|
198 } |
|
199 return false; |
|
200 } |
|
201 else |
|
202 { |
|
203 aRequest->connect(); |
|
204 if (burst) { |
|
205 iRequestBurstCount++; |
|
206 } else { |
|
207 iRequestBurstCount = 1; |
|
208 iLastRequestTime = aRequest->timeStamp(); |
|
209 } |
|
210 } |
|
211 return true; |
|
212 } |
|
213 |
|
214 void FBSession::flushRequestQueue() |
|
215 { |
|
216 while ( iRequestQueue.count() ) { |
|
217 FBRequest* request = iRequestQueue.at(0); |
|
218 if (performRequest(request, false)) { |
|
219 iRequestQueue.removeAt(0); |
|
220 } else { |
|
221 startFlushTimer(); |
|
222 break; |
|
223 } |
|
224 } |
|
225 } |
|
226 |
|
227 /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
228 // instance provate slots |
|
229 void FBSession::requestTimerReady() |
|
230 { |
|
231 flushRequestQueue(); |
|
232 } |