example/LastFmAuthApp/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, Sasken Communication Technologies Ltd
       
    14 * Description:
       
    15 * Base class of All dialogs
       
    16 */
       
    17 
       
    18 #define EMULATORTESTING 1
       
    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 #include "keys.h"
       
    39 
       
    40 static const QString kDefaultTitle = "Connect to LastFm";
       
    41 static const QString kStringBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
       
    42 /*static const QString kMinitokensuccessURL = "http://www.last.fm/api/grantaccess";//http://www.last.fm/api/auth/";
       
    43 static const QString kUrlChk = "http://www.last.fm/home";*/
       
    44 //static const QString kInvalidUrl = "http://mlogin.yahoo.com/w/login";
       
    45 
       
    46 static QNetworkAccessManager namanager;
       
    47 
       
    48 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    49 
       
    50 FBDialog::FBDialog() : iSession(FBSession::session())
       
    51 {
       
    52     createControls();
       
    53 }
       
    54 
       
    55 FBDialog::FBDialog(FBSession* aSession) : iSession ( aSession )
       
    56 {
       
    57     createControls();
       
    58 }
       
    59 void FBDialog::createControls()
       
    60 {
       
    61 	flag = true;
       
    62 	iWebView = new QWebView(this);
       
    63 	iWebSettings =  iWebView->page()->settings();
       
    64 	iWebSettings->setAttribute(QWebSettings::JavascriptEnabled, false);
       
    65 	iWebSettings->setAttribute(QWebSettings::PrintElementBackgrounds,false);
       
    66 	iWebSettings->setAttribute(QWebSettings::DnsPrefetchEnabled,true);
       
    67 	iWebSettings->setAttribute(QWebSettings::AutoLoadImages,false);
       
    68 	//iWebSettings->setAttribute(QWebSettings::JavaEnabled,false);
       
    69 
       
    70     iWebView->page()->setNetworkAccessManager(&namanager);
       
    71 
       
    72     iWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
       
    73     
       
    74     layout = new QVBoxLayout(this);
       
    75     
       
    76     progressbar = new QProgressBar(this);
       
    77     
       
    78     //scrollbar = new QScrollArea(this);
       
    79     layout->addWidget(iWebView);
       
    80     //layout->addWidget(scrollbar);
       
    81     //layout->addWidget(progressbar);
       
    82     
       
    83     setLayout(layout);
       
    84     
       
    85     progressbar->setOrientation(Qt::Horizontal);
       
    86     
       
    87 
       
    88     connect( iWebView->page(), SIGNAL(linkClicked(const QUrl &)),
       
    89                     this, SLOT(linkClicked(const QUrl &)));
       
    90 
       
    91     connect ( iWebView->page(), SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
       
    92 
       
    93     connect ( iWebView->page(), SIGNAL(loadStarted()), this, SLOT ( loadStarted()));
       
    94     
       
    95     connect ( iWebView->page(), SIGNAL(loadProgress(int)), this, SLOT ( loadProgress(int)));
       
    96     
       
    97     connect (iWebView->page()->networkAccessManager(),SIGNAL( authenticationRequired( QNetworkReply*, QAuthenticator*)),this,SLOT( slotAuthenticationRequired( QNetworkReply*, QAuthenticator*)));
       
    98     connect (iWebView->page()->networkAccessManager(),SIGNAL( sslErrors( QNetworkReply*,QList<QSslError>&)),this,SLOT( slotsslErrors( QNetworkReply*,QList<QSslError>&)) );
       
    99     connect (iWebView->page()->networkAccessManager(),SIGNAL( proxyAuthenticationRequired(QNetworkProxy&, QAuthenticator*)),this,SLOT( slotproxyAuthenticationRequired(QNetworkProxy&, QAuthenticator*)) );
       
   100 
       
   101 }
       
   102 QString FBDialog::generateURL( const QString& aUrl, const QHash<QString, QString>& aParams) const
       
   103 {
       
   104     QString url ( aUrl );
       
   105 
       
   106     QStringList pairs;
       
   107     QHashIterator<QString, QString> i(aParams);
       
   108 
       
   109     while (i.hasNext()) {
       
   110         i.next();
       
   111 
       
   112         QUrl url (i.value());
       
   113         QString pair = i.key() + "=" + url.toEncoded();
       
   114         pairs << pair.toUtf8();
       
   115     }
       
   116 
       
   117     if (pairs.count())
       
   118     {
       
   119         url = url + "?" + pairs.join("&");
       
   120     }
       
   121 
       
   122     return url;
       
   123 
       
   124 }
       
   125 
       
   126 QByteArray FBDialog::generatePostBody (const QHash<QString, QString>& aParams) const
       
   127 {
       
   128     QByteArray body;
       
   129 
       
   130     if (!aParams.count())
       
   131         return body;
       
   132 
       
   133 
       
   134     QString endLine = "\r\n--" + kStringBoundary + "\r\n", kStringBoundary;
       
   135 
       
   136     QString tmp = "--" + kStringBoundary + "\r\n";
       
   137     body.append(tmp.toUtf8());
       
   138 
       
   139 
       
   140     QHashIterator<QString, QString> i(aParams);
       
   141     while (i.hasNext()) {
       
   142         i.next();
       
   143 
       
   144         tmp = "Content-Disposition: form-data; name=\"" + i.key().toUtf8() + "\"\r\n\r\n" ;
       
   145         body.append(tmp.toUtf8());
       
   146         body.append(i.value().toUtf8());
       
   147         body.append(endLine.toUtf8());
       
   148     }
       
   149 
       
   150     return body;
       
   151 }
       
   152 
       
   153 void FBDialog::postDismissCleanup()
       
   154 {
       
   155     //accept();
       
   156     // could also be reject()?
       
   157 }
       
   158 
       
   159 void FBDialog::dismiss (bool /*aAnimated*/) {
       
   160     dialogWillDisappear();
       
   161     iLoadingUrl.clear();
       
   162 
       
   163     //todo: do some animations if aAnimated == true !
       
   164     postDismissCleanup();
       
   165 }
       
   166 
       
   167 
       
   168 void FBDialog::dismissWithSuccess( bool aSuccess, bool aAnimated)
       
   169 {
       
   170   if (aSuccess) {
       
   171         emit dialogDidSucceed();
       
   172   } else {
       
   173         emit dialogDidCancel();
       
   174   }
       
   175 
       
   176   dismiss(aAnimated);
       
   177 }
       
   178 
       
   179 void FBDialog::dismissWithError (const FBError& aError)
       
   180 {
       
   181 	Q_UNUSED(aError)
       
   182 	QMessageBox msgbox;
       
   183 	QString msg ("Error!Please try again!");
       
   184 	msgbox.setText(msg);
       
   185 }
       
   186 void FBDialog::slotAuthenticationRequired( QNetworkReply* reply, QAuthenticator* authenticator )
       
   187 	{
       
   188 		Q_UNUSED(reply)
       
   189 		Q_UNUSED(authenticator)
       
   190 		QMessageBox msgbox;
       
   191 		QString msg ("Error!Authentication Required");
       
   192 		msgbox.setText(msg);
       
   193 	}
       
   194 void FBDialog::slotsslErrors( QNetworkReply* reply, const QList<QSslError>& errors  )
       
   195 	{
       
   196 		Q_UNUSED(reply)
       
   197 		Q_UNUSED(errors)
       
   198 		QMessageBox msgbox;
       
   199 		QString msg ("Error!SSL Error");
       
   200 		msgbox.setText(msg);
       
   201 	}
       
   202 void FBDialog::slotproxyAuthenticationRequired( const QNetworkProxy& proxy, QAuthenticator* authenticator  )
       
   203 	{
       
   204 		Q_UNUSED(proxy)
       
   205 		Q_UNUSED(authenticator)
       
   206 		QMessageBox msgbox;
       
   207 		QString msg ("Error!Proxy Authenticatio Required");
       
   208 		msgbox.setText(msg);
       
   209 	}
       
   210 void FBDialog::cancel()
       
   211 {}
       
   212 
       
   213 void FBDialog::load() {}
       
   214 
       
   215 void FBDialog::show()
       
   216 {
       
   217     load();
       
   218     showMaximized();
       
   219     dialogWillAppear();
       
   220 
       
   221 }
       
   222 
       
   223 void FBDialog::loadURL(const QString& aUrl, QNetworkAccessManager::Operation aMethod, const QHash<QString, QString>& aGetParams, const QHash<QString, QString>&  aPostParams)
       
   224 {   
       
   225 	Q_UNUSED(aMethod)
       
   226 	Q_UNUSED(aPostParams)
       
   227     QNetworkCookieJar* cookieJar = iWebView->page()->networkAccessManager()->cookieJar();
       
   228     QByteArray body;
       
   229 
       
   230    iLoadingUrl = generateURL(aUrl, aGetParams);
       
   231 
       
   232     QUrl url (iLoadingUrl);
       
   233     QNetworkRequest request(aUrl);
       
   234  
       
   235 	proxysettings();
       
   236 	if(flag)
       
   237 		{
       
   238 			urlShoot = iLoadingUrl;
       
   239 			qDebug()<< "urlShoot : " << urlShoot;
       
   240 		}
       
   241     
       
   242     qDebug()<< "Check URL : " << iLoadingUrl;
       
   243     iWebView->load(url);
       
   244 }
       
   245 void FBDialog::proxysettings()
       
   246 {
       
   247 #ifdef EMULATORTESTING
       
   248 	qDebug()<<"proxysettings";
       
   249 	
       
   250 	// Reading the keys, CSM Stubbed - START
       
   251 	QFile file("c://data//DoNotShare.txt");
       
   252 	if (!file.open(QIODevice::ReadOnly))
       
   253 		{
       
   254 		qDebug()<<"File to read the windows username and password could not be opened, returning!!!";
       
   255 		return;
       
   256 		}
       
   257 	
       
   258 	QByteArray arr = file.readAll();
       
   259 	QList<QByteArray> list = arr.split(' ');
       
   260 	file.close();
       
   261 	
       
   262 	QString username(list[0]);
       
   263 	QString password(list[1]);
       
   264 	
       
   265     QString httpProxy = "10.1.0.214";//ipwproxy.sasken.com
       
   266     QString httpPort = "3128";
       
   267 
       
   268     QString httpUser =username;/* This could be taken thru an QDialog implmentation to remove the Hard coding */
       
   269     QString httpPass =password;/* This could be taken thru an QDialog implmentation to remove the Hard coding */
       
   270 
       
   271     /*==Classes used from Network Module==*/
       
   272     QNetworkProxy proxy;
       
   273 
       
   274     proxy.setType(QNetworkProxy::HttpProxy);
       
   275     proxy.setHostName(httpProxy);
       
   276     proxy.setPort(httpPort.toInt());
       
   277     proxy.setUser(httpUser);
       
   278     proxy.setPassword(httpPass);
       
   279 
       
   280     QNetworkProxy::setApplicationProxy(proxy);
       
   281 #endif
       
   282 }
       
   283 void FBDialog::dialogWillAppear() {}
       
   284 
       
   285 void FBDialog::dialogWillDisappear() {}
       
   286 
       
   287 void FBDialog::dialogDidSucceed (const QUrl& /*aUrl*/) {
       
   288   dismissWithSuccess(true,true);
       
   289 }
       
   290 
       
   291 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   292 void FBDialog::linkClicked ( const QUrl & url )
       
   293  {
       
   294 
       
   295         qDebug() << "Loading the url: " <<  url;
       
   296         
       
   297 		proxysettings();
       
   298         iWebView->load(url);
       
   299 }
       
   300 
       
   301 void FBDialog::loadStarted()
       
   302 {
       
   303 	qDebug() << "Enter: FBDialog::loadStarted()";
       
   304     qDebug() << "Load started: " << iWebView->url();
       
   305     qDebug() << "Before: layout->addWidget(progressbar);";
       
   306     layout->addWidget(progressbar);
       
   307     qDebug() << "After: layout->addWidget(progressbar);";
       
   308     qDebug() << "Before: progressbar->setVisible(true);";
       
   309     progressbar->setVisible(true);
       
   310     qDebug() << "After: progressbar->setVisible(true);";
       
   311     qDebug() << "Exit: FBDialog::loadStarted()";
       
   312     
       
   313 }
       
   314 void FBDialog::loadProgress(int progress)
       
   315 {
       
   316 	qDebug()<<"Enter: FBDialog::loadProgress(int progress)";
       
   317 	progressbar->setValue(progress);
       
   318 	qDebug()<<"Exit: FBDialog::loadProgress(int progress)";
       
   319 }
       
   320 
       
   321 void FBDialog::GetSessionKey(const QUrl& aUrl)
       
   322 {
       
   323 	Q_UNUSED(aUrl)
       
   324 }
       
   325 void FBDialog::connectToGetSession()
       
   326 {
       
   327 
       
   328 }
       
   329 void FBDialog::loadFinished ( bool ok )
       
   330 {
       
   331 	qDebug()<<"Enter: FBDialog::loadFinished ( bool ok )";
       
   332     qDebug() << "Load " << (ok ? "" : "un") << "successfull for: " << iWebView->url();
       
   333     layout->removeWidget(progressbar);
       
   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->close();
       
   343 				QMessageBox msgbox;
       
   344 				QString msg ("Complete the Authorization?");
       
   345 				msgbox.setText(msg);
       
   346 				msgbox.exec();
       
   347 				
       
   348 				connectToGetSession();				
       
   349 				
       
   350         	}
       
   351         else if(url.compare(kUrlChk)==0)
       
   352         	{
       
   353 				iWebView->load(urlShoot);
       
   354         	}
       
   355     }
       
   356     else
       
   357     {
       
   358         FBError err;
       
   359         dismissWithError(err);
       
   360     }
       
   361 }