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