example/FlickrAuthApp/src/baseDialog.cpp
changeset 17 106a4bfcb866
child 26 83d6a149c755
equal deleted inserted replaced
16:b78fa4cdbf2b 17:106a4bfcb866
       
     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 #define EMULATORTESTING
       
    19 
       
    20 #include <QNetworkCookie>
       
    21 #include <QNetworkRequest>
       
    22 #include <QNetworkReply>
       
    23 #include <QWebFrame>
       
    24 #include <QFile>
       
    25 #include <QDesktopServices>
       
    26 #include <QResizeEvent>
       
    27 #include <QDebug>
       
    28 #include <qmessagebox.h>
       
    29 #include <QNetworkAccessManager>
       
    30 #include <QPropertyAnimation>
       
    31  #include <qnetworkproxy.h>
       
    32 #include <qdesktopwidget.h>
       
    33 #include <qapplication.h>
       
    34 //#include <qboxlayout.h>
       
    35 #include "baseDialog.h"
       
    36 #include "sessionSP.h"
       
    37 #include "errorCodes.h"
       
    38 
       
    39 static const QString kDefaultTitle = "Connect to Facebook";
       
    40 static const QString kStringBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
       
    41 static const QString kMinitokensuccessURL = "http://m.flickr.com/services/auth/";
       
    42 static const QString kInvalidUrl = "http://mlogin.yahoo.com/w/login";
       
    43 
       
    44 static QNetworkAccessManager namanager;
       
    45 
       
    46 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    47 
       
    48 FBDialog::FBDialog() : iSession(FBSession::session()), /*iWebView ( this  ),*/ iIgnorePageLoadCompleteEvent( false )
       
    49 {
       
    50     createControls();
       
    51 }
       
    52 
       
    53 FBDialog::FBDialog(FBSession* aSession) : iSession ( aSession ), /*iWebView ( this  ) ,*/ iIgnorePageLoadCompleteEvent ( false )
       
    54 {
       
    55     createControls();
       
    56 }
       
    57 void FBDialog::createControls()
       
    58 {
       
    59 	iWebView = new QWebView(this);
       
    60     iWebView->page()->setNetworkAccessManager(&namanager);
       
    61 
       
    62     iWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
       
    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(), SIGNAL(urlChanged ( const QUrl & url )), this, SLOT (urlChanged ( const QUrl & url ) ));
       
    87     
       
    88     connect (iWebView->page()->networkAccessManager(),SIGNAL( authenticationRequired( QNetworkReply*, QAuthenticator*)),this,SLOT( slotAuthenticationRequired( QNetworkReply*, QAuthenticator*)));
       
    89     connect (iWebView->page()->networkAccessManager(),SIGNAL( sslErrors( QNetworkReply*,QList<QSslError>&)),this,SLOT( slotsslErrors( QNetworkReply*,QList<QSslError>&)) );
       
    90     connect (iWebView->page()->networkAccessManager(),SIGNAL( proxyAuthenticationRequired(QNetworkProxy&, QAuthenticator*)),this,SLOT( slotproxyAuthenticationRequired(QNetworkProxy&, QAuthenticator*)) );
       
    91 
       
    92 }
       
    93 QString FBDialog::generateURL( const QString& aUrl, const QHash<QString, QString>& aParams) const
       
    94 {
       
    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     QByteArray body;
       
   120 
       
   121     if (!aParams.count())
       
   122         return body;
       
   123 
       
   124 
       
   125     QString endLine = "\r\n--" + kStringBoundary + "\r\n", kStringBoundary;
       
   126 
       
   127     QString tmp = "--" + kStringBoundary + "\r\n";
       
   128     body.append(tmp.toUtf8());
       
   129 
       
   130 
       
   131     QHashIterator<QString, QString> i(aParams);
       
   132     while (i.hasNext()) {
       
   133         i.next();
       
   134 
       
   135         tmp = "Content-Disposition: form-data; name=\"" + i.key().toUtf8() + "\"\r\n\r\n" ;
       
   136         body.append(tmp.toUtf8());
       
   137         body.append(i.value().toUtf8());
       
   138         body.append(endLine.toUtf8());
       
   139     }
       
   140 
       
   141     return body;
       
   142 }
       
   143 
       
   144 void FBDialog::postDismissCleanup()
       
   145 {
       
   146     //accept();
       
   147     // could also be reject()?
       
   148 }
       
   149 
       
   150 void FBDialog::dismiss (bool /*aAnimated*/) {
       
   151     dialogWillDisappear();
       
   152     iLoadingUrl.clear();
       
   153 
       
   154     //todo: do some animations if aAnimated == true !
       
   155     postDismissCleanup();
       
   156 }
       
   157 
       
   158 
       
   159 void FBDialog::dismissWithSuccess( bool aSuccess, bool aAnimated)
       
   160 {
       
   161   if (aSuccess) {
       
   162         emit dialogDidSucceed();
       
   163   } else {
       
   164         emit dialogDidCancel();
       
   165   }
       
   166 
       
   167   dismiss(aAnimated);
       
   168 }
       
   169 
       
   170 void FBDialog::dismissWithError (const FBError& aError)
       
   171 {
       
   172 	QMessageBox msgbox;
       
   173 	QString msg ("Error!Please try again!");
       
   174 	msgbox.setText(msg);
       
   175 }
       
   176 void FBDialog::slotAuthenticationRequired( QNetworkReply* reply, QAuthenticator* authenticator )
       
   177 	{
       
   178 		QMessageBox msgbox;
       
   179 		QString msg ("Error!Authentication Required");
       
   180 		msgbox.setText(msg);
       
   181 	}
       
   182 void FBDialog::slotsslErrors( QNetworkReply* reply, const QList<QSslError>& errors  )
       
   183 	{
       
   184 		QMessageBox msgbox;
       
   185 		QString msg ("Error!SSL Error");
       
   186 		msgbox.setText(msg);
       
   187 	}
       
   188 void FBDialog::slotproxyAuthenticationRequired( const QNetworkProxy& proxy, QAuthenticator* authenticator  )
       
   189 	{
       
   190 		QMessageBox msgbox;
       
   191 		QString msg ("Error!Proxy Authenticatio Required");
       
   192 		msgbox.setText(msg);
       
   193 	}
       
   194 void FBDialog::cancel()
       
   195 {}
       
   196 
       
   197 void FBDialog::load() {}
       
   198 
       
   199 void FBDialog::show()
       
   200 {
       
   201 
       
   202     load();
       
   203     showMaximized();
       
   204     dialogWillAppear();
       
   205 
       
   206 }
       
   207 
       
   208 void FBDialog::loadURL(const QString& aUrl, QNetworkAccessManager::Operation aMethod, const QHash<QString, QString>& aGetParams, const QHash<QString, QString>&  aPostParams)
       
   209 {   
       
   210     //proxysettings();
       
   211     iIgnorePageLoadCompleteEvent = false;
       
   212 
       
   213     QNetworkCookieJar* cookieJar = iWebView->page()->networkAccessManager()->cookieJar();
       
   214     QByteArray body;
       
   215 
       
   216    // iLoadingUrl = aUrl;
       
   217    iLoadingUrl = generateURL(aUrl, aGetParams);
       
   218 
       
   219     // This "test cookie" is required by login.php, or it complains that you need to enable JavaScript
       
   220     QNetworkCookie testCookie ("test_cookie", "1");
       
   221     testCookie.setDomain ( "www.flickr.com" );
       
   222     testCookie.setPath ( "/" );
       
   223 
       
   224     QList<QNetworkCookie> cookieList;
       
   225     cookieList.append(testCookie);
       
   226 
       
   227     cookieJar->setCookiesFromUrl ( cookieList, QUrl(iLoadingUrl) );
       
   228 
       
   229     QUrl url (iLoadingUrl);
       
   230     QNetworkRequest request(aUrl);
       
   231 
       
   232     if (aMethod == QNetworkAccessManager::PostOperation)
       
   233     {
       
   234         const QString contentType = "multipart/form-data; boundary=" + kStringBoundary;
       
   235         request.setHeader (QNetworkRequest::ContentTypeHeader, contentType);
       
   236         body = generatePostBody (aPostParams);
       
   237     }
       
   238     
       
   239 	proxysettings();
       
   240 
       
   241     
       
   242     qDebug()<< "Check URL : " << iLoadingUrl;
       
   243     
       
   244     //iWebView->load ( request, aMethod, body);
       
   245     iWebView->load(url);
       
   246     qDebug() << "Loading url: " << iLoadingUrl;
       
   247 }
       
   248 void FBDialog::proxysettings()
       
   249 {
       
   250 #ifdef EMULATORTESTING
       
   251 	qDebug()<<"proxysettings";
       
   252 	
       
   253 	// Reading the keys, CSM Stubbed - START
       
   254 	QFile file("c://data//DoNotShare.txt");
       
   255 	if (!file.open(QIODevice::ReadOnly))
       
   256 		{
       
   257 		qDebug()<<"File to read the windows username and password could not be opened, returning!!!";
       
   258 		return;
       
   259 		}
       
   260 	
       
   261 	QByteArray arr = file.readAll();
       
   262 	QList<QByteArray> list = arr.split(' ');
       
   263 	file.close();
       
   264 	
       
   265 	QString username(list[0]);
       
   266 	QString password(list[1]);
       
   267 	
       
   268     QString httpProxy = "10.1.0.214";//ipwproxy.sasken.com
       
   269     QString httpPort = "3128";
       
   270 
       
   271     QString httpUser =username;/* This could be taken thru an QDialog implmentation to remove the Hard coding */
       
   272     QString httpPass =password;/* This could be taken thru an QDialog implmentation to remove the Hard coding */
       
   273 
       
   274     /*==Classes used from Network Module==*/
       
   275     QNetworkProxy proxy;
       
   276 
       
   277     proxy.setType(QNetworkProxy::HttpProxy);
       
   278     proxy.setHostName(httpProxy);
       
   279     proxy.setPort(httpPort.toInt());
       
   280     proxy.setUser(httpUser);
       
   281     proxy.setPassword(httpPass);
       
   282 
       
   283     QNetworkProxy::setApplicationProxy(proxy);
       
   284 #endif
       
   285 }
       
   286 void FBDialog::dialogWillAppear() {}
       
   287 
       
   288 void FBDialog::dialogWillDisappear() {}
       
   289 
       
   290 void FBDialog::dialogDidSucceed (const QUrl& /*aUrl*/) {
       
   291   dismissWithSuccess(true,true);
       
   292 }
       
   293 
       
   294 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   295 void FBDialog::linkClicked ( const QUrl & url )
       
   296  {
       
   297 
       
   298         qDebug() << "Loading the url: " <<  url;
       
   299         
       
   300 		proxysettings();
       
   301         iWebView->load(url);
       
   302 }
       
   303 
       
   304 void FBDialog::loadStarted()
       
   305 {
       
   306     qDebug() << "Load started: " << iWebView->url();
       
   307     progressbar->setVisible(true);
       
   308 }
       
   309 void FBDialog::loadProgress(int progress)
       
   310 {
       
   311 	progressbar->setValue(progress);
       
   312 }
       
   313 /*void FBDialog::urlChanged ( const QUrl & url )
       
   314 	{
       
   315 		if(url.toString().contains(kInvalidUrl))
       
   316 			{
       
   317 				QUrl newurl(iWebView->url().toString());
       
   318 				qDebug()<<"url changed"<<iWebView->url().toString()<<"\n";
       
   319 				iWebView->setUrl(newurl);
       
   320 				iWebView->load(iWebView->url().toString());
       
   321 			}
       
   322 	}*/
       
   323 void FBDialog::GetSessionKey(const QUrl& aUrl)
       
   324 {
       
   325 
       
   326 }
       
   327 void FBDialog::connectToGetFullToken()
       
   328 {
       
   329 
       
   330 }
       
   331 void FBDialog::loadFinished ( bool ok )
       
   332 {
       
   333     qDebug() << "Load " << (ok ? "" : "un") << "successfull for: " << iWebView->url();
       
   334     progressbar->setVisible(false);
       
   335     if (ok)
       
   336     {
       
   337         QString url = iWebView->url().toString();
       
   338         qDebug() << "Loaded URL " << url;
       
   339         qDebug() << "To be compared URL " << kMinitokensuccessURL;
       
   340         if(url.compare(kMinitokensuccessURL) == 0)
       
   341         	{
       
   342 				iWebView->setHidden(true);
       
   343 				QMessageBox msgbox;
       
   344 				QString msg ("Complete the Authorization?");
       
   345 				msgbox.setText(msg);
       
   346 				msgbox.exec();
       
   347 				
       
   348 				connectToGetFullToken();				
       
   349 				
       
   350         	}
       
   351     }
       
   352     else
       
   353     {
       
   354         if (iIgnorePageLoadCompleteEvent)
       
   355             return;
       
   356 
       
   357         FBError err;
       
   358         dismissWithError(err);
       
   359     }
       
   360 }