wlanutilities/wlanlogin/wlanloginapp/src/wlanloginengine.cpp
changeset 39 7b3e49e4608a
child 43 72ebcbb64834
equal deleted inserted replaced
36:682dd021f9be 39:7b3e49e4608a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Wlan Login engine class implementation
       
    16 * 
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <QObject>
       
    21 #include <QString>
       
    22 #include <QUrl>
       
    23 #include <HbMessagebox>
       
    24 #include <HbAction>
       
    25 #include <qnetworkconfigmanager.h>
       
    26 #include <qnetworkconfiguration.h>
       
    27 #include <qnetworksession.h>
       
    28 
       
    29 //User includes
       
    30 #include "wlanloginengine.h"
       
    31 #include "wlanloginapplication.h"
       
    32 #include "wlanloginmainwindow.h"
       
    33 #include "ictsqtwrapper.h"
       
    34 #include "wlanloginnetworkaccessmanager.h"
       
    35 
       
    36 #include "OstTraceDefinitions.h"
       
    37 #ifdef OST_TRACE_COMPILER_IN_USE
       
    38 #include "wlanloginengineTraces.h"
       
    39 #endif
       
    40 
       
    41 /*!
       
    42     \class WlanLoginEngine
       
    43     \brief WLAN Login engine class implementation.
       
    44 */
       
    45 
       
    46 // External function prototypes
       
    47 
       
    48 // Local constants
       
    49 
       
    50 // ======== LOCAL FUNCTIONS ========
       
    51 
       
    52 // ======== MEMBER FUNCTIONS ========
       
    53 
       
    54 /*!
       
    55     Constructor.
       
    56  */
       
    57 WlanLoginEngine::WlanLoginEngine(QObject *parent): 
       
    58     QObject(parent),
       
    59     mEngineState(WaitingForStart),
       
    60     mServiceRequestCompleted(false),
       
    61     mNetConfigurationManager(new QNetworkConfigurationManager(this)),
       
    62     mNetworkAccessManager(new WlanLoginNetworkAccessManager(this)),
       
    63     mNetworkSession(NULL),
       
    64     mIctsWrapper(NULL),
       
    65     mIctsFirstRun(true),
       
    66     mIap(0),
       
    67     mNetId(0),
       
    68     mRedirectionUrl(QUrl(""))            
       
    69 {
       
    70     OstTraceFunctionEntry0(WLANLOGINENGINE_WLANLOGINENGINE_ENTRY);
       
    71     
       
    72     bool connectStatus = connect(
       
    73         mNetConfigurationManager,
       
    74         SIGNAL(updateCompleted()),
       
    75         this, 
       
    76         SLOT(handleUpdateCompleted()));  
       
    77     Q_ASSERT(connectStatus == true);
       
    78             
       
    79     OstTraceFunctionExit0(WLANLOGINENGINE_WLANLOGINENGINE_EXIT);
       
    80 }
       
    81 
       
    82 
       
    83 /*!
       
    84     Destructor.
       
    85  */
       
    86 WlanLoginEngine::~WlanLoginEngine()
       
    87 {
       
    88     OstTraceFunctionEntry0(WLANLOGINENGINE_DESTRUCTOR_ENTRY);
       
    89    
       
    90     mNetworkSession->close();
       
    91     
       
    92     OstTraceFunctionExit0(WLANLOGINENGINE_DESTRUCTOR_EXIT);
       
    93 }
       
    94 
       
    95 
       
    96 /*!
       
    97     Function that returns pointer to network access manager.
       
    98     
       
    99     @return Pointer to WlanLoginNetworkAccessManager
       
   100  */
       
   101 WlanLoginNetworkAccessManager* WlanLoginEngine::networkAccessManager() const
       
   102 {
       
   103     return mNetworkAccessManager;
       
   104 }
       
   105 
       
   106 
       
   107 /*!
       
   108     Function that returns current state of the engine.
       
   109     
       
   110     @return engine state
       
   111  */
       
   112 WlanLoginEngine::EngineState WlanLoginEngine::engineState()
       
   113 {
       
   114     return mEngineState;    
       
   115 }
       
   116 
       
   117 
       
   118 /*!
       
   119     Function that sets the current state of the engine.
       
   120     
       
   121     @param [in] newState New state of the engine to be set
       
   122  */
       
   123 void WlanLoginEngine::setEngineState(WlanLoginEngine::EngineState newState)
       
   124 {
       
   125     mEngineState = newState;
       
   126 }
       
   127 
       
   128 
       
   129 /*!
       
   130     This slot handles startLogin signal from Qt Highway service provider
       
   131     
       
   132     @param [in] iapId Internet access point ID for a connection that we should attach to'
       
   133     @param [in] netId Network Id for the connection to be used
       
   134     @param [in] redirectionUrl The URL that we were redirected to by the first internet connectivity test
       
   135     
       
   136  */
       
   137 void WlanLoginEngine::handleStartLogin(int iapId, int netId, QUrl redirectionUrl)
       
   138 {
       
   139     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLESTARTLOGIN_ENTRY);
       
   140    
       
   141     mIap = iapId; 
       
   142     OstTrace1(
       
   143         TRACE_NORMAL,
       
   144         WLANLOGINENGINE_HANDLESTARTLOGIN_IAP_TRACE,
       
   145         "WlanLoginEngine::handleStartLogin;mIap=%d",
       
   146         mIap);
       
   147   
       
   148     mNetId = netId;
       
   149     OstTrace1(
       
   150         TRACE_NORMAL,
       
   151         WLANLOGINENGINE_HANDLESTARTLOGIN_NETID_TRACE,
       
   152         "WlanLoginEngine::handleStartLogin;mNetId=%d",
       
   153         mNetId);
       
   154        
       
   155     mRedirectionUrl = redirectionUrl;
       
   156     
       
   157 #ifdef OST_TRACE_COMPILER_IN_USE
       
   158     QString url = mRedirectionUrl.toString();
       
   159     TPtrC tmp(url.utf16(),url.length() );
       
   160     
       
   161     OstTraceExt1(
       
   162         TRACE_NORMAL,
       
   163         WLANLOGINENGINE_HANDLESTARTLOGIN_URL_TRACE, 
       
   164         "WlanLoginEngine::handleStartLogin;mRedirectionUrl=%S",
       
   165         tmp);
       
   166 #endif
       
   167         
       
   168     //Create ICTS instance as we know needed parameters now
       
   169     mIctsWrapper = new IctsWrapper(mIap, mNetId, this);
       
   170     
       
   171     bool connectStatus = connect(
       
   172         mIctsWrapper,
       
   173         SIGNAL(connectivityTestResult(IctsWrapper::ConnectivityTestResult, QUrl)), 
       
   174         this,
       
   175         SLOT(handleConnectivityTestResult(IctsWrapper::ConnectivityTestResult, QUrl)));  
       
   176     Q_ASSERT(connectStatus == true);
       
   177     
       
   178     //Update network configurations as only states of the cellular IAP's are reliable 
       
   179     //after creating network configuration manager
       
   180     
       
   181     setEngineState(UpdatingNetworkConfigurations);
       
   182     mNetConfigurationManager->updateConfigurations();
       
   183     
       
   184     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLESTARTLOGIN_EXIT);
       
   185 }
       
   186 
       
   187 /*!
       
   188     This slot handles stopLogin signal from Qt Highway service provider
       
   189         
       
   190  */
       
   191 void WlanLoginEngine::handleStopLogin()
       
   192 {
       
   193     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLESTOPLOGIN_ENTRY);
       
   194 
       
   195     // Exit application
       
   196     qApp->quit();
       
   197 
       
   198     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLESTOPLOGIN_EXIT);
       
   199 }
       
   200 
       
   201 
       
   202 /*!
       
   203     This slot handles updateCompleted signal from QNetworkConfigManager
       
   204         
       
   205  */
       
   206 void WlanLoginEngine::handleUpdateCompleted()
       
   207 {
       
   208    OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLEUPDATECOMPLETED_ENTRY);
       
   209    
       
   210    //States of the available configurations are now updated and
       
   211    //we can open the network session to provided IAP
       
   212    
       
   213    openSession();
       
   214 
       
   215    OstTraceFunctionExit0(WLANLOGINENGINE_HANDLEUPDATECOMPLETED_EXIT);
       
   216 }
       
   217 
       
   218 /*!
       
   219     This function handles opening of the network session to the specified IAP.
       
   220     If IAP is not already in connected state, function completes service request
       
   221     with connection dropped status and show a message box to user.
       
   222         
       
   223  */
       
   224 void WlanLoginEngine::openSession()
       
   225 {
       
   226     OstTraceFunctionEntry0(WLANLOGINENGINE_OPENSESSION_ENTRY);
       
   227 
       
   228     setEngineState(WlanLoginEngine::OpeningNetworkSession);
       
   229     
       
   230     //Get configuration that matches the provided IAP
       
   231     QNetworkConfiguration netConfiguration = mNetConfigurationManager->configurationFromIdentifier(QString::number(mIap));    
       
   232     
       
   233     OstTrace1(
       
   234         TRACE_NORMAL,
       
   235         WLANLOGINENGINE_OPENSESSION_CONFIGURATION_IDENTIFIER_TRACE,
       
   236         "WlanLoginEngine::openSession;netConfiguration identifier=%d",
       
   237         netConfiguration.identifier().toInt());
       
   238     
       
   239     OstTrace1(TRACE_NORMAL,
       
   240         WLANLOGINENGINE_OPENSESSION_CONFIGURATION_STATE_TRACE,
       
   241         "WlanLoginEngine::openSession;netConfiguration state=%d",
       
   242         netConfiguration.state());
       
   243     
       
   244 #ifdef OST_TRACE_COMPILER_IN_USE
       
   245     QString string = netConfiguration.name();
       
   246     TPtrC tmp(string.utf16(),string.length() );
       
   247     
       
   248     OstTraceExt1(
       
   249         TRACE_NORMAL,
       
   250         WLANLOGINENGINE_OPENSESSION_CONFIGURATION_NAME_TRACE, 
       
   251         "WlanLoginEngine::openSession;netConfiguration name=%S",
       
   252         tmp );
       
   253        
       
   254     string = netConfiguration.bearerName();
       
   255     tmp.Set(string.utf16(),string.length());
       
   256     
       
   257     OstTraceExt1(
       
   258         TRACE_NORMAL,
       
   259         WLANLOGINENGINE_OPENSESSION_CONFIGURATION_BEARER_NAME_TRACE, 
       
   260         "WlanLoginEngine::openSession;netConfiguration bearerName=%S",
       
   261         tmp );
       
   262 #endif
       
   263     
       
   264     //Check that configuration state is active before opening session
       
   265     //Otherwise show disconnected message box
       
   266     
       
   267     if (netConfiguration.state() == QNetworkConfiguration::Active)
       
   268         {
       
   269         //Create network session to IAP that was provided in startup
       
   270         mNetworkSession = new QNetworkSession(netConfiguration, this);
       
   271         
       
   272         bool connectStatus = connect(
       
   273             mNetworkSession,
       
   274             SIGNAL(opened()),
       
   275             this, 
       
   276             SLOT(handleSessionOpened()));
       
   277         Q_ASSERT(connectStatus == true);
       
   278         
       
   279         connectStatus = connect(
       
   280             mNetworkSession,
       
   281             SIGNAL(stateChanged(QNetworkSession::State)), 
       
   282             this,
       
   283             SLOT(handleSessionStateChanged(QNetworkSession::State)));       
       
   284         Q_ASSERT(connectStatus == true);
       
   285         
       
   286         connectStatus = connect(
       
   287             mNetworkSession,
       
   288             SIGNAL(error(QNetworkSession::SessionError)), 
       
   289             this,
       
   290             SLOT(handleSessionError(QNetworkSession::SessionError)));   
       
   291         Q_ASSERT(connectStatus == true);
       
   292         
       
   293         mNetworkSession->open();
       
   294         } else {
       
   295             showConnectionDroppedMessageBox();
       
   296         }
       
   297     
       
   298     OstTraceFunctionExit0(WLANLOGINENGINE_OPENSESSION_EXIT);
       
   299 }
       
   300 
       
   301 
       
   302 /*!
       
   303     This function handles sessionOpened signal from QNetworkSession. It informs view that
       
   304     connection is ready to be used.
       
   305         
       
   306  */
       
   307 void WlanLoginEngine::handleSessionOpened()
       
   308 {
       
   309     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLESESSIONOPENED_ENTRY);
       
   310 
       
   311     setEngineState(NetworkSessionOpened);
       
   312     //Send indication to view that connection is ready
       
   313     emit connectionReady(mRedirectionUrl);
       
   314 
       
   315     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLESESSIONOPENED_EXIT);
       
   316 }
       
   317 
       
   318 
       
   319 /*!
       
   320     This function handles sessionStateChanged signal from QNetworkSession.
       
   321     @param [in] newState New state of the session  
       
   322  */
       
   323 void WlanLoginEngine::handleSessionStateChanged(QNetworkSession::State newState)
       
   324 {
       
   325     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLESESSIONSTATECHANGED_ENTRY);
       
   326 
       
   327     OstTrace1(
       
   328         TRACE_NORMAL,
       
   329         WLANLOGINENGINE_HANDLESESSIONSTATECHANGED_STATE_TRACE,
       
   330         "WlanLoginEngine::handleSessionStateChanged;newState=%d",
       
   331         newState );
       
   332     
       
   333     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLESESSIONSTATECHANGED_EXIT);
       
   334 }
       
   335 
       
   336 
       
   337 /*!
       
   338     This function handles sessionError signal from QNetworkSession
       
   339     @param [in] error session error     
       
   340  */
       
   341 void WlanLoginEngine::handleSessionError(QNetworkSession::SessionError error)
       
   342 {
       
   343     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLESESSIONERROR_ENTRY);
       
   344         
       
   345     OstTrace1(
       
   346         TRACE_NORMAL,
       
   347         WLANLOGINENGINE_SESSION_ERROR_TRACE,
       
   348         "WlanLoginEngine::handleSessionError;error=%d",
       
   349         error);
       
   350     
       
   351     switch (engineState()) {    
       
   352     case OpeningNetworkSession:
       
   353         showConnectionDroppedMessageBox();
       
   354         break;
       
   355         
       
   356     case NetworkSessionOpened:
       
   357     case IctsRan:
       
   358         if (error == QNetworkSession::SessionAbortedError ) {
       
   359             showConnectionDroppedMessageBox();
       
   360         }
       
   361         break;
       
   362         
       
   363     case RunningIcts:
       
   364         if (error == QNetworkSession::SessionAbortedError ) {
       
   365             stopIcts();
       
   366             showConnectionDroppedMessageBox();
       
   367         }
       
   368         break;
       
   369         
       
   370     default:
       
   371         break;
       
   372     }
       
   373         
       
   374     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLESESSIONERROR_EXIT);
       
   375 }
       
   376 
       
   377 /*!
       
   378     This function handles startIcts signal and starts connectivity test in polling mode
       
   379         
       
   380  */
       
   381 void WlanLoginEngine::handleStartIcts()
       
   382 {
       
   383     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLESTARTICTS_ENTRY);
       
   384     
       
   385     if (!mIctsWrapper->isPolling()) {
       
   386         setEngineState(RunningIcts);
       
   387         mIctsWrapper->startPolling(totalPollingTime, pollingInterval);
       
   388     }
       
   389 
       
   390     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLESTARTICTS_EXIT);
       
   391 }
       
   392 
       
   393 /*!
       
   394     This function stops internet connectivity test
       
   395         
       
   396  */
       
   397 void WlanLoginEngine::stopIcts()
       
   398 {
       
   399     OstTraceFunctionEntry0(WLANLOGINENGINE_STOPICTS_ENTRY);
       
   400 
       
   401     if (mIctsWrapper->isPolling()) {
       
   402         mIctsWrapper->stopPolling();
       
   403     }
       
   404 
       
   405     OstTraceFunctionExit0(WLANLOGINENGINE_STOPICTS_EXIT);
       
   406 }
       
   407 
       
   408 /*!
       
   409     This slot handles connectivityTestResult signal from ICTS wrapper
       
   410     @param [in] result Result of the internet connectivity test
       
   411     @param [in] redirectionUrl The URL that we are being redirected, valid only if result is HttpAuthenticationNeeded
       
   412  */
       
   413 
       
   414 void WlanLoginEngine::handleConnectivityTestResult(IctsWrapper::ConnectivityTestResult result, QUrl redirectUrl)
       
   415 {
       
   416     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLECONNECTIVITYTESTRESULT_ENTRY);
       
   417 
       
   418     Q_UNUSED(redirectUrl);
       
   419     
       
   420     setEngineState(IctsRan);
       
   421     mIctsFirstRun = false;
       
   422     
       
   423     if (result == IctsWrapper::ConnectionOk){
       
   424         emit ictsOk();
       
   425     }
       
   426     
       
   427     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLECONNECTIVITYTESTRESULT_EXIT);
       
   428 }
       
   429 
       
   430 /*!
       
   431     This slot handles cancelTriggered signal from the view
       
   432         
       
   433  */
       
   434 void WlanLoginEngine::handleCancelTriggered()
       
   435 {
       
   436     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLECANCELTRIGGERED_ENTRY);
       
   437     
       
   438     //Stop ICTS if it is running:
       
   439     if (engineState() == RunningIcts) {
       
   440         stopIcts();
       
   441         if (mIctsFirstRun) {
       
   442             emitCompleteServiceRequest(WlanLoginService::WlanLoginStatusCancel);
       
   443         } else {
       
   444             //Previous ICTS must have failed as cancel action can't be selected from UI if
       
   445             //it has passed
       
   446             emitCompleteServiceRequest(WlanLoginService::WlanLoginStatusFailed);
       
   447         }         
       
   448     } else if (engineState() == IctsRan) {
       
   449         //ICTS must have failed as cancel action can't be selected from UI if
       
   450         //it has passed
       
   451         emitCompleteServiceRequest(WlanLoginService::WlanLoginStatusFailed);
       
   452     } else {
       
   453         //ICTS not run yet
       
   454         emitCompleteServiceRequest(WlanLoginService::WlanLoginStatusCancel); 
       
   455     }
       
   456 
       
   457     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLECANCELTRIGGERED_EXIT);
       
   458 }
       
   459 
       
   460 /*!
       
   461     This slot handles nextTriggered signal from the view
       
   462         
       
   463  */
       
   464 void WlanLoginEngine::handleNextTriggered()
       
   465 {
       
   466     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLENEXTTRIGGERED_ENTRY);
       
   467         
       
   468     emitCompleteServiceRequest(WlanLoginService::WlanLoginStatusNext);
       
   469     
       
   470     //put application to background:
       
   471     WlanLoginApplication* app = static_cast<WlanLoginApplication *>(this->parent());    
       
   472     app->mainWindow()->lower();
       
   473     
       
   474     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLENEXTTRIGGERED_EXIT);
       
   475 }
       
   476 
       
   477 /*!
       
   478     This slot handles continueTriggered signal from the view
       
   479         
       
   480  */
       
   481 void WlanLoginEngine::handleContinueTriggered()
       
   482 {
       
   483     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLECONTINUETRIGGERED_ENTRY);
       
   484     
       
   485     //put application to background:
       
   486     WlanLoginApplication* app = static_cast<WlanLoginApplication *>(this->parent());
       
   487     app->mainWindow()->lower();
       
   488     
       
   489     OstTraceFunctionExit0(WLANLOGINENGINE_HANDLECONTINUETRIGGERED_EXIT);
       
   490 }
       
   491 
       
   492 /*!
       
   493     This function shows connection dropped message box to the user
       
   494         
       
   495  */
       
   496 void WlanLoginEngine::showConnectionDroppedMessageBox()
       
   497 {
       
   498     OstTraceFunctionEntry0(WLANLOGINENGINE_SHOWCONNECTIONDROPPEDMESSAGEBOX_ENTRY);
       
   499     
       
   500     HbMessageBox *messageBox = new HbMessageBox(hbTrId("txt_occ_info_wlan_connection_closed_login_applica"));
       
   501     messageBox->setAttribute(Qt::WA_DeleteOnClose);
       
   502     messageBox->setTimeout(messageBoxTimeout);
       
   503     messageBox->open(this,SLOT(connectionDroppedMessageBoxClosed(HbAction*)));
       
   504     
       
   505     OstTraceFunctionExit0(WLANLOGINENGINE_SHOWCONNECTIONDROPPEDMESSAGEBOX_EXIT);
       
   506 }
       
   507 
       
   508 void WlanLoginEngine::connectionDroppedMessageBoxClosed(HbAction* action)
       
   509 {
       
   510     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLEMESSAGEBOXCLOSED_ENTRY);
       
   511     
       
   512     Q_UNUSED(action);
       
   513         
       
   514     if (mServiceRequestCompleted == false ) {
       
   515         emitCompleteServiceRequest(WlanLoginService::WlanLoginStatusConnectionDropped);
       
   516                 
       
   517     } else {
       
   518         //If Qt Highway service request is already completed when connection drops, we have to make exit by our own
       
   519         qApp->exit();
       
   520     }               
       
   521       
       
   522     OstTraceFunctionEntry0(WLANLOGINENGINE_HANDLEMESSAGEBOXCLOSED_EXIT);    
       
   523 }
       
   524 
       
   525 /*!
       
   526     This function completes Qt Highway service request
       
   527     @param [in] status completeing status of the service request   
       
   528  */
       
   529 void WlanLoginEngine::emitCompleteServiceRequest(WlanLoginService::WlanLoginStatus status )
       
   530 {
       
   531     OstTraceFunctionEntry0(WLANLOGINENGINE_EMITCOMPLETESERVICEREQUEST_ENTRY);
       
   532         
       
   533     mServiceRequestCompleted = true;
       
   534     OstTrace1(
       
   535         TRACE_BORDER,
       
   536         WLANLOGINENGINE_EMITCOMPLETESERVICEREQUEST_EMIT_TRACE,
       
   537         "WlanLoginEngine::emitCompleteServiceRequest with status:;status=%d",
       
   538         status);
       
   539     emit completeServiceRequest(status);
       
   540 
       
   541     OstTraceFunctionExit0(WLANLOGINENGINE_EMITCOMPLETESERVICEREQUEST_EXIT);
       
   542 }