|
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 * Base class of All dialogs |
|
16 */ |
|
17 |
|
18 |
|
19 #include <QNetworkCookie> |
|
20 #include <QNetworkRequest> |
|
21 #include <QNetworkReply> |
|
22 #include <QWebFrame> |
|
23 #include <QFile> |
|
24 #include <QDesktopServices> |
|
25 #include <QResizeEvent> |
|
26 #include <QDebug> |
|
27 #include <qmessagebox.h> |
|
28 #include <QNetworkAccessManager> |
|
29 #include <QPropertyAnimation> |
|
30 #include <qnetworkproxy.h> |
|
31 #include <qdesktopwidget.h> |
|
32 #include <qapplication.h> |
|
33 //#include <qboxlayout.h> |
|
34 #include "baseDialog.h" |
|
35 #include "sessionSP.h" |
|
36 #include "errorCodes.h" |
|
37 |
|
38 static const QString kDefaultTitle = "Connect to Facebook"; |
|
39 static const QString kStringBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; |
|
40 |
|
41 static QNetworkAccessManager namanager; |
|
42 |
|
43 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
44 |
|
45 FBDialog::FBDialog() : iSession(FBSession::session()), /*iWebView ( this ),*/ iIgnorePageLoadCompleteEvent( false ) |
|
46 { |
|
47 createControls(); |
|
48 } |
|
49 |
|
50 FBDialog::FBDialog(FBSession* aSession) : iSession ( aSession ), /*iWebView ( this ) ,*/ iIgnorePageLoadCompleteEvent ( false ) |
|
51 { |
|
52 createControls(); |
|
53 } |
|
54 void FBDialog::createControls() |
|
55 { |
|
56 iWebView = new QWebView(this); |
|
57 iWebView->page()->setNetworkAccessManager(&namanager); |
|
58 |
|
59 iWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); |
|
60 |
|
61 layout = new QVBoxLayout(this); |
|
62 |
|
63 |
|
64 progressbar = new QProgressBar(this); |
|
65 |
|
66 layout->addWidget(iWebView); |
|
67 layout->addWidget(progressbar); |
|
68 |
|
69 setLayout(layout); |
|
70 |
|
71 progressbar->setOrientation(Qt::Horizontal); |
|
72 |
|
73 |
|
74 connect( iWebView->page(), SIGNAL(linkClicked(const QUrl &)), |
|
75 this, SLOT(linkClicked(const QUrl &))); |
|
76 |
|
77 connect ( iWebView->page(), SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool))); |
|
78 |
|
79 connect ( iWebView->page(), SIGNAL(loadStarted()), this, SLOT ( loadStarted())); |
|
80 |
|
81 connect ( iWebView->page(), SIGNAL(loadProgress(int)), this, SLOT ( loadProgress(int))); |
|
82 |
|
83 connect (iWebView->page()->networkAccessManager(),SIGNAL( authenticationRequired( QNetworkReply*, QAuthenticator*)),this,SLOT( slotAuthenticationRequired( QNetworkReply*, QAuthenticator*))); |
|
84 connect (iWebView->page()->networkAccessManager(),SIGNAL( sslErrors( QNetworkReply*,QList<QSslError>&)),this,SLOT( slotsslErrors( QNetworkReply*,QList<QSslError>&)) ); |
|
85 connect (iWebView->page()->networkAccessManager(),SIGNAL( proxyAuthenticationRequired(QNetworkProxy&, QAuthenticator*)),this,SLOT( slotproxyAuthenticationRequired(QNetworkProxy&, QAuthenticator*)) ); |
|
86 |
|
87 } |
|
88 QString FBDialog::generateURL( const QString& aUrl, const QHash<QString, QString>& aParams) const |
|
89 { |
|
90 QString url ( aUrl ); |
|
91 |
|
92 QStringList pairs; |
|
93 QHashIterator<QString, QString> i(aParams); |
|
94 |
|
95 while (i.hasNext()) { |
|
96 i.next(); |
|
97 |
|
98 QUrl url (i.value()); |
|
99 QString pair = i.key() + "=" + url.toEncoded(); |
|
100 pairs << pair.toUtf8(); |
|
101 } |
|
102 |
|
103 if (pairs.count()) |
|
104 { |
|
105 url = url + "?" + pairs.join("&"); |
|
106 } |
|
107 |
|
108 return url; |
|
109 |
|
110 } |
|
111 |
|
112 QByteArray FBDialog::generatePostBody (const QHash<QString, QString>& aParams) const |
|
113 { |
|
114 QByteArray body; |
|
115 |
|
116 if (!aParams.count()) |
|
117 return body; |
|
118 |
|
119 |
|
120 QString endLine = "\r\n--" + kStringBoundary + "\r\n", kStringBoundary; |
|
121 |
|
122 QString tmp = "--" + kStringBoundary + "\r\n"; |
|
123 body.append(tmp.toUtf8()); |
|
124 |
|
125 |
|
126 QHashIterator<QString, QString> i(aParams); |
|
127 while (i.hasNext()) { |
|
128 i.next(); |
|
129 |
|
130 tmp = "Content-Disposition: form-data; name=\"" + i.key().toUtf8() + "\"\r\n\r\n" ; |
|
131 body.append(tmp.toUtf8()); |
|
132 body.append(i.value().toUtf8()); |
|
133 body.append(endLine.toUtf8()); |
|
134 } |
|
135 |
|
136 return body; |
|
137 } |
|
138 |
|
139 void FBDialog::postDismissCleanup() |
|
140 { |
|
141 //accept(); |
|
142 // could also be reject()? |
|
143 } |
|
144 |
|
145 void FBDialog::dismiss (bool /*aAnimated*/) { |
|
146 dialogWillDisappear(); |
|
147 iLoadingUrl.clear(); |
|
148 |
|
149 //todo: do some animations if aAnimated == true ! |
|
150 postDismissCleanup(); |
|
151 } |
|
152 |
|
153 |
|
154 void FBDialog::dismissWithSuccess( bool aSuccess, bool aAnimated) |
|
155 { |
|
156 if (aSuccess) { |
|
157 emit dialogDidSucceed(); |
|
158 } else { |
|
159 emit dialogDidCancel(); |
|
160 } |
|
161 |
|
162 dismiss(aAnimated); |
|
163 } |
|
164 |
|
165 void FBDialog::dismissWithError (const FBError& aError, bool aAnimated) |
|
166 { |
|
167 emit dialogDidFailWithError( aError ); |
|
168 dismiss(aAnimated); |
|
169 } |
|
170 void FBDialog::slotAuthenticationRequired( QNetworkReply* reply, QAuthenticator* authenticator ) |
|
171 { |
|
172 QMessageBox msgbox; |
|
173 QString msg ("Error!Authentication Required"); |
|
174 msgbox.setText(msg); |
|
175 } |
|
176 void FBDialog::slotsslErrors( QNetworkReply* reply, const QList<QSslError>& errors ) |
|
177 { |
|
178 QMessageBox msgbox; |
|
179 QString msg ("Error!SSL Error"); |
|
180 msgbox.setText(msg); |
|
181 } |
|
182 void FBDialog::slotproxyAuthenticationRequired( const QNetworkProxy& proxy, QAuthenticator* authenticator ) |
|
183 { |
|
184 QMessageBox msgbox; |
|
185 QString msg ("Error!Proxy Authenticatio Required"); |
|
186 msgbox.setText(msg); |
|
187 } |
|
188 void FBDialog::cancel() |
|
189 {} |
|
190 |
|
191 void FBDialog::load() {} |
|
192 |
|
193 void FBDialog::show() |
|
194 { |
|
195 |
|
196 load(); |
|
197 showMaximized(); |
|
198 dialogWillAppear(); |
|
199 |
|
200 } |
|
201 |
|
202 void FBDialog::loadURL(const QString& aUrl, QNetworkAccessManager::Operation aMethod, const QHash<QString, QString>& aGetParams, const QHash<QString, QString>& aPostParams) |
|
203 { |
|
204 //proxysettings(); |
|
205 iIgnorePageLoadCompleteEvent = false; |
|
206 |
|
207 QNetworkCookieJar* cookieJar = iWebView->page()->networkAccessManager()->cookieJar(); |
|
208 QByteArray body; |
|
209 |
|
210 iLoadingUrl = generateURL(aUrl, aGetParams); |
|
211 |
|
212 // This "test cookie" is required by login.php, or it complains that you need to enable JavaScript |
|
213 QNetworkCookie testCookie ("test_cookie", "1"); |
|
214 testCookie.setDomain ( "www.facebook.com" ); |
|
215 testCookie.setPath ( "/" ); |
|
216 |
|
217 QList<QNetworkCookie> cookieList; |
|
218 cookieList.append(testCookie); |
|
219 |
|
220 cookieJar->setCookiesFromUrl ( cookieList, QUrl(iLoadingUrl) ); |
|
221 |
|
222 QUrl url (iLoadingUrl); |
|
223 QNetworkRequest request(url); |
|
224 |
|
225 if (aMethod == QNetworkAccessManager::PostOperation) |
|
226 { |
|
227 const QString contentType = "multipart/form-data; boundary=" + kStringBoundary; |
|
228 request.setHeader (QNetworkRequest::ContentTypeHeader, contentType); |
|
229 body = generatePostBody (aPostParams); |
|
230 } |
|
231 #ifdef __WINSCW__ |
|
232 proxysettings(); |
|
233 #endif |
|
234 |
|
235 qDebug()<< "Check URL : " << iLoadingUrl; |
|
236 |
|
237 iWebView->load ( request, aMethod, body); |
|
238 |
|
239 } |
|
240 void FBDialog::proxysettings() |
|
241 { |
|
242 |
|
243 qDebug()<<"proxysettings"; |
|
244 QString httpProxy = "10.1.0.214";//ipwproxy.sasken.com |
|
245 QString httpPort = "3128"; |
|
246 |
|
247 QString httpUser ="";/* This could be taken thru an QDialog implmentation to remove the Hard coding */ |
|
248 QString httpPass ="";/* This could be taken thru an QDialog implmentation to remove the Hard coding */ |
|
249 |
|
250 /*==Classes used from Network Module==*/ |
|
251 QNetworkProxy proxy; |
|
252 |
|
253 proxy.setType(QNetworkProxy::HttpProxy); |
|
254 proxy.setHostName(httpProxy); |
|
255 proxy.setPort(httpPort.toInt()); |
|
256 proxy.setUser(httpUser); |
|
257 proxy.setPassword(httpPass); |
|
258 |
|
259 QNetworkProxy::setApplicationProxy(proxy); |
|
260 |
|
261 } |
|
262 void FBDialog::dialogWillAppear() {} |
|
263 |
|
264 void FBDialog::dialogWillDisappear() {} |
|
265 |
|
266 void FBDialog::dialogDidSucceed (const QUrl& /*aUrl*/) { |
|
267 dismissWithSuccess(true,true); |
|
268 } |
|
269 |
|
270 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
271 void FBDialog::linkClicked ( const QUrl & url ) |
|
272 { |
|
273 |
|
274 qDebug() << "Loading the url: " << url; |
|
275 proxysettings(); |
|
276 iWebView->load(url); |
|
277 } |
|
278 |
|
279 void FBDialog::loadStarted() |
|
280 { |
|
281 qDebug() << "Load started: " << iWebView->url(); |
|
282 progressbar->setVisible(true); |
|
283 } |
|
284 void FBDialog::loadProgress(int progress) |
|
285 { |
|
286 progressbar->setValue(progress); |
|
287 } |
|
288 void FBDialog::GetSessionKey(const QUrl& aUrl) |
|
289 { |
|
290 |
|
291 } |
|
292 void FBDialog::loadFinished ( bool ok ) |
|
293 { |
|
294 qDebug() << "Load " << (ok ? "" : "un") << "successfull for: " << iWebView->url(); |
|
295 progressbar->setVisible(false); |
|
296 if (ok) |
|
297 { |
|
298 QString PAth = iWebView->url().toString(); |
|
299 PAth = PAth.mid(0,PAth.indexOf("?")); |
|
300 |
|
301 qDebug() << "Path is : " << PAth; |
|
302 |
|
303 if(KSuccessurl.compare(PAth) == 0){ |
|
304 |
|
305 QByteArray URL = iWebView->url().encodedQuery(); |
|
306 qDebug() << "Encoded Query is : " << URL; |
|
307 |
|
308 if(URL.contains("auth_token")) |
|
309 { |
|
310 iWebView->setHidden(true); |
|
311 QMessageBox msgbox; |
|
312 QString msg ("Logged in Success!!!Complete the Authorization?"); |
|
313 msgbox.setText(msg); |
|
314 if(msgbox.exec() == QMessageBox::Ok){ |
|
315 GetSessionKey(iWebView->url()); |
|
316 }else{ |
|
317 |
|
318 } |
|
319 } |
|
320 else |
|
321 { |
|
322 iWebView->setHidden(true); |
|
323 QMessageBox msgbox; |
|
324 QString msg ("Permissions Success!!!continue login?"); |
|
325 msgbox.setText(msg); |
|
326 if(msgbox.exec() == QMessageBox::Ok){ |
|
327 iWebView->setHidden(false); |
|
328 load(); |
|
329 }else{ |
|
330 |
|
331 } |
|
332 } |
|
333 } |
|
334 /*else if (URL.contains("read_stream") || URL.contains("publish_stream") || URL.contains("offline_access")) |
|
335 { |
|
336 iWebView->setHidden(true); |
|
337 QMessageBox msgbox; |
|
338 QString msg ("Permissions Success!!!continue login?"); |
|
339 msgbox.setText(msg); |
|
340 if(msgbox.exec() == QMessageBox::Ok){ |
|
341 iWebView->setHidden(false); |
|
342 load(); |
|
343 }else{ |
|
344 |
|
345 } |
|
346 }*/ |
|
347 } |
|
348 else |
|
349 { |
|
350 if (iIgnorePageLoadCompleteEvent) |
|
351 return; |
|
352 |
|
353 FBError err; |
|
354 dismissWithError(err, true); |
|
355 } |
|
356 } |