voipplugins/accountcreationplugin/engine/src/acpcontroller.cpp
changeset 0 a4daefaec16c
child 8 7117cbf1600a
equal deleted inserted replaced
-1:000000000000 0:a4daefaec16c
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  Implements CAcpController methods
       
    15 *
       
    16 */
       
    17 
       
    18 #include <apgcli.h>
       
    19 #include <apgtask.h>
       
    20 #include <eikenv.h>
       
    21 #include <utf.h>
       
    22 #include <pathinfo.h> // For getting phone memory boot path.
       
    23 #include <f32file.h>  // For getting phone memory boot path.
       
    24 #include <sip.h>
       
    25 #include <sipprofileregistry.h>
       
    26 #include <sipmanagedprofileregistry.h>
       
    27 #include <sipprofile.h>
       
    28 #include <sipmanagedprofile.h>
       
    29 #include <XdmSettingsApi.h>
       
    30 #include <pressettingsapi.h>
       
    31 #include <centralrepository.h>
       
    32 #include <CWPEngine.h>
       
    33 #include <wspdecoder.h>
       
    34 #include <PnpUtilImpl.h>
       
    35 #include <imcvcodc.h>
       
    36 
       
    37 #include "acpprovider.h"
       
    38 #include "acpcontroller.h"
       
    39 #include "acpxmlhandler.h"
       
    40 #include "acphttphandler.h"
       
    41 #include "accountcreationpluginlogger.h"
       
    42 #include "macpcontrollerobserver.h"
       
    43 #include "acpbrowserparams.h"
       
    44 #include "acpimagehandler.h"
       
    45 #include "acpprivatecrkeys.h"
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CAcpController::CAcpController
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CAcpController::CAcpController( MAcpControllerObserver& aObserver ) 
       
    52     : iObserver( aObserver ),
       
    53     iActiveIndex( KErrNone ),
       
    54     iSisInstallation( EFalse )
       
    55     {
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CAcpController::ConstructL
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 void CAcpController::ConstructL()
       
    63     {
       
    64     ACPLOG( "CAcpController::ConstructL begin" );
       
    65 
       
    66     iParser = CAcpXmlHandler::NewL( *this );
       
    67     iHttpHandler = CAcpHttpHandler::NewL( *this );
       
    68     iData = HBufC8::NewL( KBufferSize );
       
    69     iImageHandler = CAcpImageHandler::NewL( *this );
       
    70     User::LeaveIfError( iFs.Connect() );
       
    71     iSessionId = HBufC8::NewL( 0 );
       
    72 
       
    73     ACPLOG( "CAcpController::ConstructL end" );
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CAcpController::NewL
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 EXPORT_C CAcpController* CAcpController::NewL( 
       
    81     MAcpControllerObserver& aObserver )
       
    82     {
       
    83     CAcpController* self = CAcpController::NewLC( aObserver );
       
    84     CleanupStack::Pop( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CAcpController::NewLC
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CAcpController* CAcpController::NewLC( 
       
    94     MAcpControllerObserver& aObserver )
       
    95     {
       
    96     CAcpController* self = new ( ELeave ) CAcpController( aObserver );
       
    97     CleanupStack::PushL( self );
       
    98     self->ConstructL();
       
    99     return self;
       
   100     }
       
   101 
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CAcpController::~CAcpController
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 CAcpController::~CAcpController()
       
   108     {
       
   109     ACPLOG( "CAcpController::~CAcpController begin" );
       
   110 
       
   111     iFs.Close();
       
   112     iProviders.ResetAndDestroy();
       
   113     delete iParser;
       
   114     delete iHttpHandler;
       
   115     delete iData;
       
   116 
       
   117 #ifdef __PLUG_AND_PLAY_MOBILE_SERVICES    
       
   118     delete iPnpmsparams;
       
   119 #endif
       
   120    
       
   121     delete iImageHandler;
       
   122     delete iSessionId;
       
   123     delete iSipModel;
       
   124 
       
   125     ACPLOG( "CAcpController::~CAcpController end" );
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CAcpController::FetchProviderListFromNetworkL
       
   130 // Fetches provider list from network and parses it.
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C void CAcpController::FetchProviderListFromNetworkL()
       
   134     {
       
   135     ACPLOG( "CAcpController::FetchProviderListFromNetworkL begin" );
       
   136 
       
   137     // Create SIP profile registry. From now on we'll get SIP events.
       
   138     iSipModel = CSIPManagedProfileRegistry::NewL( *this );
       
   139     
       
   140     // Delete old data, otherwise Refresh won't work.
       
   141     delete iData;
       
   142     iData = NULL;
       
   143     iData = HBufC8::NewL( KBufferSize );    
       
   144     iFilename.Zero();
       
   145     iProviders.ResetAndDestroy();
       
   146 
       
   147     // 'Restart' XML parser.
       
   148     delete iParser;
       
   149     iParser = NULL;
       
   150     iParser = CAcpXmlHandler::NewL( *this );
       
   151 
       
   152     // Get server URL from CenRep.
       
   153     HBufC* url = HBufC::NewLC( KNameMaxLength * 2 );
       
   154     CRepository* cenrep = CRepository::NewLC( KAccountCreationSettingsUid );
       
   155     
       
   156     TPtr urlPtr( url->Des() );
       
   157     cenrep->Get( KAccountCreationPluginServerUri, urlPtr );
       
   158     
       
   159     CleanupStack::PopAndDestroy( cenrep );    
       
   160 
       
   161     // Descriptor for body data.
       
   162     TBuf8<KDefaultBufferSize> postData8;
       
   163 
       
   164     // Get the download parameters.
       
   165     FetchParametersL( urlPtr ); 
       
   166     
       
   167     // Convert unicode descriptor into UTF8.
       
   168     HBufC8 *url8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L( *url );
       
   169     CleanupStack::PushL( url8 );
       
   170     
       
   171     // Start dowloading provider list from network.
       
   172     iHttpHandler->GetDataL( *url8, KXmlMimeType, postData8, EFalse,
       
   173         KNullDesC8 );
       
   174 
       
   175     CleanupStack::PopAndDestroy( 2, url ); // url8, url
       
   176 
       
   177     // Set request type for the http observer because of downloading XML
       
   178     // file from network.
       
   179     // From now on, response events comes to NotifyHttpEvent, NotifyHttpError, 
       
   180     // NotifyBodyReceived and NotifyContentTypeReceived.
       
   181     iType = EXml;   
       
   182 
       
   183     // Reset active index.
       
   184     SaveActiveIndex( 0 );
       
   185 
       
   186     ACPLOG( "CAcpController::FetchProviderListFromNetworkL end" );
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 // CAcpController::FetchSisFileFromNetworkL
       
   191 // Fetches provider list from network and parses it.
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 EXPORT_C void CAcpController::FetchSisFileFromNetworkL( TDesC8& aSisUrl )
       
   195     {
       
   196     ACPLOG( "CAcpController::FetchSisFileFromNetworkL begin" );
       
   197 
       
   198     // Delete old data.
       
   199     delete iData;
       
   200     iData = NULL;
       
   201     iData = HBufC8::NewL( KBufferSize ); 
       
   202     
       
   203     // Descriptor for body data.
       
   204     TBuf8<KDefaultBufferSize> postData8; 
       
   205 
       
   206     iFs.Delete( KSisFile ); // Old file should delete at first.
       
   207     iWs.Create( iFs , KSisFile, EFileWrite );// Create the file as requested.
       
   208     iFilename.Copy( KSisFile );
       
   209 
       
   210     // Start downloading sis file from network. Use saved session id.
       
   211     iHttpHandler->GetDataL( aSisUrl, KSisMimeType, postData8, EFalse,
       
   212         *iSessionId, ETrue );
       
   213 
       
   214     // Set request type for the HTTP observer because of downloading SIS file
       
   215     // from network.
       
   216     // From now on, response events comes to NotifyHttpEvent, NotifyHttpError, 
       
   217     // NotifyBodyReceived and NotifyContentTypeReceived.
       
   218     iType = ESis;    
       
   219     
       
   220     // Reset error flag.
       
   221     iErrorDownloading = EFalse;
       
   222 
       
   223     ACPLOG( "CAcpController::FetchSisFileFromNetworkL end" );
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // CAcpController::FetchIconFileFromNetworkL
       
   228 // Fetches provider list from network and parses it.
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 EXPORT_C void CAcpController::FetchIconFileFromNetworkL( TDesC8& aIconUrl )
       
   232     {
       
   233     ACPLOG( "CAcpController::FetchIconFileFromNetworkL begin" );
       
   234     ACPLOG2( " - fetching: %S", &aIconUrl );
       
   235     
       
   236     if ( aIconUrl.Length() )
       
   237         {
       
   238         // Descriptor for downloading icon file.
       
   239         TBuf<KDefaultBufferSize> url;
       
   240 
       
   241         // Copy the icon address into the descriptor with default size (256).
       
   242         url.Copy( aIconUrl.Right( KDefaultBufferSize ) );
       
   243 
       
   244         // Descriptor for body data.
       
   245         TBuf8<KDefaultBufferSize> postData8;
       
   246 
       
   247         // Use saved session id when fetching icons.
       
   248         iHttpHandler->GetDataL( aIconUrl, KIconMimeType, postData8, ETrue,
       
   249             *iSessionId );
       
   250 
       
   251         // Set request type for the HTTP observer because of downloading icon
       
   252         // file from network.
       
   253         // From now on, response events comes to NotifyHttpEvent,
       
   254         // NotifyHttpError, NotifyBodyReceived and NotifyContentTypeReceived.
       
   255         iType = EIcon; 
       
   256         }
       
   257     
       
   258     ACPLOG( "CAcpController::FetchIconFileFromNetworkL end" );
       
   259     }
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // CAcpController::CountOfProviders
       
   263 // Returns count of providers in providers array.
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 EXPORT_C TInt CAcpController::CountOfProviders() const
       
   267     {
       
   268     TInt count = iProviders.Count();
       
   269     ACPLOG2( "CAcpController::CountOfProviders: count=%d", count );
       
   270     return count;
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // CAcpController::ProviderNameFromIndexL
       
   275 // Returns pointer to provider name by index.
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 EXPORT_C const TPtrC CAcpController::ProviderNameFromIndexL(
       
   279     TInt aIndex ) const
       
   280     {
       
   281     if ( 0 > aIndex || aIndex >= iProviders.Count() ) 
       
   282         {
       
   283         User::Leave( KErrArgument );
       
   284         }
       
   285     CAcpProvider& provider = *iProviders[aIndex];
       
   286     return provider.ProviderName();
       
   287     }
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // CAcpController::ProviderSisUrlFromIndexL
       
   291 // Returns pointer to provider sisurl by index.
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 EXPORT_C const TPtrC8 CAcpController::ProviderSisUrlFromIndexL( 
       
   295     TInt aIndex ) const
       
   296     {
       
   297     if ( 0 > aIndex || aIndex >= iProviders.Count() )
       
   298         {
       
   299         User::Leave( KErrArgument );
       
   300         }
       
   301     CAcpProvider& provider = *iProviders[aIndex];
       
   302     return provider.SisUrl();   
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------------------------
       
   306 // CAcpController::ProviderActivateUrlFromIndexL
       
   307 // Returns pointer to provider sisurl by index.
       
   308 // ---------------------------------------------------------------------------
       
   309 //
       
   310 EXPORT_C const TPtrC8 CAcpController::ProviderActivationUrlFromIndexL( 
       
   311     TInt aIndex ) const
       
   312     {
       
   313     if ( 0 > aIndex || aIndex >= iProviders.Count() )
       
   314         {
       
   315         User::Leave( KErrArgument ); 
       
   316         }
       
   317     CAcpProvider& provider = *iProviders[aIndex];
       
   318     return provider.ActivationUrl(); 
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // CAcpController::ProviderCreateUrlFromIndexL
       
   323 // Returns pointer to provider sisurl by index.
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 EXPORT_C const TPtrC8 CAcpController::ProviderCreationUrlFromIndexL( 
       
   327     TInt aIndex ) const
       
   328     {
       
   329     if ( 0 > aIndex || aIndex >= iProviders.Count() )
       
   330         {
       
   331         User::Leave( KErrArgument );
       
   332         }
       
   333     CAcpProvider& provider = *iProviders[aIndex];
       
   334     return provider.CreationUrl();    
       
   335     }
       
   336 
       
   337 // ---------------------------------------------------------------------------
       
   338 // CAcpController::ProviderDescriptionFromIndexL
       
   339 // Returns pointer to provider description by index.
       
   340 // ---------------------------------------------------------------------------
       
   341 //
       
   342 EXPORT_C const TPtrC CAcpController::ProviderDescriptionFromIndexL( 
       
   343     TInt aIndex ) const
       
   344     {
       
   345     if ( 0 > aIndex || aIndex >= iProviders.Count() )
       
   346         {
       
   347         User::Leave( KErrArgument );
       
   348         }
       
   349     CAcpProvider& provider = *iProviders[aIndex];
       
   350     return provider.ProviderDescription();    
       
   351     }
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // CAcpController::ProviderTypeFromIndexL
       
   355 // Returns pointer to provider Type by index.
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 EXPORT_C const TPtrC CAcpController::ProviderTypeFromIndexL( 
       
   359     TInt aIndex ) const
       
   360     {
       
   361     if ( 0 > aIndex || aIndex >= iProviders.Count() )
       
   362         {
       
   363         User::Leave( KErrArgument );
       
   364         }
       
   365     CAcpProvider& provider = *iProviders[aIndex];
       
   366     return provider.ProviderType();    
       
   367     }
       
   368 
       
   369 // ---------------------------------------------------------------------------
       
   370 // CAcpController::ProviderIconUrlFromIndexL
       
   371 // Returns pointer to provider Type by index.
       
   372 // ---------------------------------------------------------------------------
       
   373 //
       
   374 EXPORT_C const TPtrC8 CAcpController::ProviderIconUrlFromIndexL( 
       
   375     TInt aIndex ) const
       
   376     {
       
   377     if ( 0 > aIndex || aIndex >= iProviders.Count() )
       
   378         {
       
   379         User::Leave( KErrArgument );
       
   380         }
       
   381 
       
   382     CAcpProvider& provider = *iProviders[aIndex];
       
   383     return provider.IconUrl();
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 // CAcpController::SaveActiveIndex
       
   388 // Stores active list index in provider list view to member data.
       
   389 // ---------------------------------------------------------------------------
       
   390 //
       
   391 EXPORT_C void CAcpController::SaveActiveIndex( TInt aIndex )
       
   392     {
       
   393     iActiveIndex = aIndex;
       
   394     }
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // CAcpController::ActiveIndex
       
   398 // Returns active list index from member data.
       
   399 // ---------------------------------------------------------------------------
       
   400 //
       
   401 EXPORT_C TInt CAcpController::ActiveIndex() const
       
   402     {
       
   403     return iActiveIndex;
       
   404     }    
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 // CAcpController::FetchParametersL
       
   408 // ---------------------------------------------------------------------------
       
   409 //
       
   410 EXPORT_C void CAcpController::FetchParametersL( TDes& aUrl )
       
   411     {
       
   412     ACPLOG( "CAcpController::FetchParametersL begin" );
       
   413 
       
   414 #ifdef __PLUG_AND_PLAY_MOBILE_SERVICES    
       
   415     if ( !iPnpmsparams )
       
   416         {
       
   417         iPnpmsparams = CACPBrowserParams::NewL();
       
   418         }
       
   419 
       
   420     // This is going to append detail data after the requested address because
       
   421     // of downloading data from network later on.
       
   422     iPnpmsparams->GetParameters( aUrl );
       
   423 #endif
       
   424 
       
   425     ACPLOG( "CAcpController::FetchParametersL end" );
       
   426     }
       
   427 
       
   428 // ---------------------------------------------------------------------------
       
   429 // CAcpController::CancelHttpRequest
       
   430 // Cancels ongoing HTTP request (transaction).
       
   431 // ---------------------------------------------------------------------------
       
   432 //
       
   433 EXPORT_C void CAcpController::CancelHttpRequest()
       
   434     {
       
   435     ACPLOG( "CAcpController::CancelHttpRequest begin" );
       
   436 
       
   437     iWs.Close();
       
   438     if ( iHttpHandler )
       
   439         {
       
   440         iHttpHandler->CancelTransaction();
       
   441         }
       
   442     
       
   443     ACPLOG( "CAcpController::CancelHttpRequest end" );
       
   444     }
       
   445 
       
   446 // ---------------------------------------------------------------------------
       
   447 // CAcpController::FetchActionFileL
       
   448 // Starts downloading action (wbxml) file from the server.
       
   449 // ---------------------------------------------------------------------------
       
   450 //
       
   451 EXPORT_C void CAcpController::FetchActionFileL( const TDesC8& aUrl )
       
   452     {
       
   453     ACPLOG( "CAcpController::FetchActionFileL begin" );
       
   454     
       
   455     if ( !aUrl.Length() )
       
   456         {
       
   457         User::Leave( KErrArgument );
       
   458         }
       
   459     
       
   460     delete iData;
       
   461     iData = NULL;
       
   462     iData = HBufC8::NewL( KBufferSize );   
       
   463 
       
   464     iType = EAction;
       
   465     iHttpHandler->GetDataL( aUrl, KActionMimeType, *iData, EFalse,
       
   466         *iSessionId );
       
   467     
       
   468     // Reset error flag.
       
   469     iErrorDownloading = EFalse;
       
   470     
       
   471     ACPLOG( "CAcpController::FetchActionFileL end" );
       
   472     }
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // CAcpController::SessionId
       
   476 // Returns session id received from the server.
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 EXPORT_C const TDesC8& CAcpController::SessionId()
       
   480     {
       
   481     return *iSessionId;
       
   482     }
       
   483 
       
   484 // ---------------------------------------------------------------------------
       
   485 // CAcpController::ProviderBitmapFromIndexL
       
   486 // Returns pointer to provider icon by index.
       
   487 // ---------------------------------------------------------------------------
       
   488 //
       
   489 EXPORT_C void CAcpController::ProviderBitmapsFromIndexL(
       
   490     TInt aIndex, CFbsBitmap*& aBitmap, CFbsBitmap*& aMask ) const
       
   491     {
       
   492     ACPLOG2( "CAcpController::ProviderBitmapsFromIndexL (%d)", aIndex );
       
   493     
       
   494     // Check whether the index in range or not.
       
   495     if ( aIndex < 0 || aIndex >= iProviders.Count() ) 
       
   496         {
       
   497         // Not in range next leaves with error
       
   498         User::Leave( KErrArgument );
       
   499         }
       
   500 
       
   501     // Get provider.
       
   502     CAcpProvider& provider = *iProviders[aIndex];
       
   503     
       
   504     // Set bitmaps.
       
   505     provider.GetBitmaps( aBitmap, aMask );
       
   506     
       
   507     ACPLOG( "CAcpController::ProviderBitmapsFromIndexL out" );
       
   508     }
       
   509 
       
   510 // ---------------------------------------------------------------------------
       
   511 // CAcpController::CreateFileFromDataL
       
   512 // Creates file from data.
       
   513 // ---------------------------------------------------------------------------
       
   514 //
       
   515 void CAcpController::CreateFileFromDataL( const TDesC& aFilename )
       
   516     {
       
   517     ACPLOG2( "CAcpController::CreateFileFromDataL file=%S", &aFilename );
       
   518 
       
   519     RFileWriteStream ws; // Create stream for writing data into the file
       
   520     CleanupClosePushL( ws );
       
   521 
       
   522     iFs.Delete( aFilename ); // Old file should delete at first.
       
   523     ws.Create( iFs , aFilename, EFileWrite );// Create the file as requested.
       
   524     ws.WriteL( iData->Des() );// Write requested data into the file.
       
   525     ws.CommitL(); // Ensure that the stream is not contain data anymore.
       
   526     CleanupStack::PopAndDestroy( &ws );
       
   527 
       
   528     // Delete content of the descriptor so it's ready to receive data again
       
   529     // from network.
       
   530     TPtr8 dataPtr( iData->Des() );
       
   531     dataPtr.Delete( 0, iData->Length() );
       
   532     }
       
   533 
       
   534 // ---------------------------------------------------------------------------
       
   535 // CAcpController::HandleActionFileL
       
   536 // Parses the received action file for wbxml content and sends it to
       
   537 // provisioning engine.
       
   538 // ---------------------------------------------------------------------------
       
   539 //
       
   540 void CAcpController::HandleActionFileL()
       
   541     {
       
   542     ACPLOG( "CAcpController::HandleActionFileL begin" );
       
   543     
       
   544     if ( !iData || !iData->Length() )
       
   545         {
       
   546         User::Leave( KErrArgument );
       
   547         }
       
   548 
       
   549     // These values are from PnPMS documentation.
       
   550     const TInt KContentTypeLenPos = 5;
       
   551     const TInt KSkippableBytes = 6;
       
   552     
       
   553     // Parse content from the wrapper.
       
   554     TUint32 headersLen( 0 );
       
   555     TUint32 contentLen( 0 );
       
   556     TUint8 contentTypeLen( (*iData)[KContentTypeLenPos] );
       
   557 
       
   558     TPtrC8 decodeBuf( iData->Ptr() + KSkippableBytes + contentTypeLen );
       
   559     TWspPrimitiveDecoder decoder( decodeBuf );
       
   560     User::LeaveIfError( decoder.UintVar( headersLen ) );
       
   561     ACPLOG2( " - headers length: %d", headersLen );
       
   562     
       
   563     User::LeaveIfError( decoder.UintVar( contentLen ) );
       
   564     ACPLOG2( " - content length: %d", contentLen );
       
   565     if ( !contentLen || !headersLen )
       
   566         {
       
   567         User::Leave( KErrCorrupt );
       
   568         }
       
   569     
       
   570     // Get data and header pointers.
       
   571     const TInt dataOffset( iData->Length() - contentLen );
       
   572     const TUint8* dataPtr( iData->Ptr() + dataOffset ); 
       
   573     const TInt headerOffset( iData->Length() - contentLen - headersLen );
       
   574     const TUint8* headerPtr( iData->Ptr() + headerOffset );
       
   575     
       
   576     TPtrC8 wbxmlPtr( dataPtr, contentLen ); // Pointer to wbxml data.
       
   577     TPtrC8 hdrPtr( headerPtr, headersLen ); // Pointer to headers.
       
   578 
       
   579     // Validate data with PnPUtil.
       
   580     CPnpUtilImpl* pnpUtil = CPnpUtilImpl::NewLC();
       
   581     
       
   582     HBufC8* nonce = HBufC8::NewLC( KNonceLength );
       
   583     TPtr8 noncePtr( nonce->Des() );
       
   584     pnpUtil->GetNonceL( noncePtr );
       
   585     
       
   586     _LIT8( KSignatureValue, "SignValue:" );
       
   587     _LIT8( KDigestValue, "DigValue:" );
       
   588 
       
   589     TPtrC8 digestEnc( GetHeaderParamL( hdrPtr, KDigestValue ) );
       
   590     TPtrC8 signatureEnc( GetHeaderParamL( hdrPtr, KSignatureValue ) );
       
   591     
       
   592     if ( pnpUtil->VerifySignatureL(
       
   593         digestEnc, signatureEnc, wbxmlPtr, *nonce ) )
       
   594         {
       
   595         ACPLOG( " - signature verified" );
       
   596         }
       
   597     else
       
   598         {
       
   599         ACPLOG( " - cannot verify signature" );
       
   600         User::Leave( KErrCorrupt );
       
   601         }
       
   602     
       
   603     CleanupStack::PopAndDestroy( 2, pnpUtil );    
       
   604     
       
   605     // Import the OMA CP message.
       
   606     CWPEngine* provEngine = CWPEngine::NewLC();
       
   607     provEngine->ImportDocumentL( wbxmlPtr );
       
   608     
       
   609     // Populate OMA message to adapters.
       
   610     provEngine->PopulateL();
       
   611     
       
   612     // Save OMA message.
       
   613     const TInt itemCount = provEngine->ItemCount();
       
   614     for ( TInt counter = 0; counter < itemCount; counter++ )
       
   615         {
       
   616         provEngine->SaveL( counter );
       
   617         }
       
   618 
       
   619     CleanupStack::PopAndDestroy( provEngine );
       
   620     
       
   621     iObserver.NotifyProvisioningCompleted();
       
   622 
       
   623     ACPLOG( "CAcpController::HandleActionFileL end" );
       
   624     }
       
   625 
       
   626 // ---------------------------------------------------------------------------
       
   627 // CAcpController::NextIndexWithIconUrl
       
   628 // Returns next index with an icon URL in the providers list.
       
   629 // ---------------------------------------------------------------------------
       
   630 //
       
   631 TInt CAcpController::NextIndexWithIconUrl( TPtrC8& aResultUrl )
       
   632     {
       
   633     ACPLOG( "CAcpController::NextIndexWithIconUrl begin" );
       
   634     
       
   635     TInt ret( KErrNotFound );
       
   636 
       
   637     const TInt startIndex( iActiveIndex );
       
   638     for ( TInt i = startIndex; i < iProviders.Count(); i++ )
       
   639         {
       
   640         TPtrC8 iconUrl;
       
   641         TRAPD( error, iconUrl.Set( ProviderIconUrlFromIndexL( i ) ) );
       
   642         if ( KErrNone == error && iconUrl.Length() )
       
   643             {
       
   644             SaveActiveIndex( i );
       
   645             aResultUrl.Set( iconUrl );
       
   646             ret = i;
       
   647             break;
       
   648             }
       
   649         }
       
   650     
       
   651     ACPLOG2( "CAcpController::NextIndexWithIconUrl end (%d)", ret );
       
   652     return ret;
       
   653     }
       
   654 
       
   655 // ---------------------------------------------------------------------------
       
   656 // CAcpController::GetHeaderParamL
       
   657 // Parses a specific header value from head wrapper format.
       
   658 // ---------------------------------------------------------------------------
       
   659 //
       
   660 TPtrC8 CAcpController::GetHeaderParamL( const TDesC8& aHeaders,
       
   661     const TDesC8& aParam )
       
   662     {
       
   663     ACPLOG( "CAcpController::GetHeaderParamL begin" );
       
   664     
       
   665     if ( !aHeaders.Length() || !aParam.Length() )
       
   666         {
       
   667         User::Leave( KErrArgument );
       
   668         }
       
   669     
       
   670     TInt paramLoc = aHeaders.Find( aParam );
       
   671     
       
   672     if ( KErrNotFound == paramLoc )
       
   673         {
       
   674         ACPLOG( " - error: Given parameter not found in headers" );
       
   675         User::Leave( KErrNotFound );
       
   676         }
       
   677     else
       
   678         {
       
   679         paramLoc = paramLoc + aParam.Length();
       
   680         }
       
   681     
       
   682     TPtrC8 paramToEndPtr( aHeaders.Mid( paramLoc ) );
       
   683     TInt paramLen = paramToEndPtr.Locate( '\n' );
       
   684     if ( KErrNotFound == paramLen )
       
   685         {
       
   686         paramLen = aHeaders.Length() - paramLoc;
       
   687         }
       
   688     ACPLOG2( " - parameter length: %d", paramLen );
       
   689     
       
   690     TPtrC8 param( aHeaders.Ptr() + paramLoc, paramLen );
       
   691 
       
   692     ACPLOG( "CAcpController::GetHeaderParamL end" );
       
   693     return param;
       
   694     }
       
   695 
       
   696 // ---------------------------------------------------------------------------
       
   697 // CAcpController::NotifyParsingCompleted
       
   698 // From MAcpXmlHandlerObserver.
       
   699 // ---------------------------------------------------------------------------
       
   700 //
       
   701 void CAcpController::NotifyParsingCompleted( TInt aError )
       
   702     {
       
   703     ACPLOG( "CAcpController::NotifyParsingCompleted begin" );
       
   704 
       
   705     iFs.Delete( iFilename ); 
       
   706     
       
   707     // Forward error code to the observer and return.
       
   708     if ( KErrNone != aError )
       
   709         {
       
   710         iObserver.NotifyProviderListReady( aError );
       
   711         return;
       
   712         }
       
   713 
       
   714     // Descriptor for downloading icon file from network.
       
   715     SaveActiveIndex( 0 );
       
   716     TPtrC8 iconUrl;
       
   717     TInt index = NextIndexWithIconUrl( iconUrl );
       
   718     
       
   719     if ( KErrNotFound != index )
       
   720         {
       
   721         // Provider with icon URL found, start downloading.
       
   722         TRAP( aError, FetchIconFileFromNetworkL( iconUrl ) );
       
   723         if ( KErrNone != aError )
       
   724             {
       
   725             iObserver.NotifyProviderListReady( aError );
       
   726             CancelHttpRequest();
       
   727             }
       
   728         }
       
   729     else
       
   730         {
       
   731         // None of the providers contains icons -> loading done.
       
   732         iObserver.NotifyProviderListReady( KErrNone );
       
   733         CancelHttpRequest();
       
   734         }
       
   735 
       
   736     ACPLOG( "CAcpController::NotifyParsingCompleted end" );
       
   737     }
       
   738 
       
   739 // ---------------------------------------------------------------------------
       
   740 // CAcpController::NotifyParsedProviderL
       
   741 // From MAcpXmlHandlerObserver.
       
   742 // ---------------------------------------------------------------------------
       
   743 //
       
   744 void CAcpController::NotifyParsedProviderL( const CAcpProvider& aProvider )
       
   745     {
       
   746     // To save a data from the received provider then new instace of provider
       
   747     // must be created at first.
       
   748     CAcpProvider* provider = CAcpProvider::NewLC();
       
   749     provider->CopyL( aProvider );
       
   750     iProviders.Append( provider );
       
   751     CleanupStack::Pop( provider );
       
   752 
       
   753     ACPLOG( "CAcpController::NotifyParsedProvider: New provider added!" );
       
   754     }
       
   755 
       
   756 // ---------------------------------------------------------------------------
       
   757 // CAcpController::NotifyHttpError
       
   758 // From MAcpHttpHandlerObserver.
       
   759 // ---------------------------------------------------------------------------
       
   760 //
       
   761 void CAcpController::NotifyHttpError( TInt aError )
       
   762     {
       
   763     ACPLOG2( "CAcpController::NotifyHttpError: Error occurred: %d", aError );
       
   764     iObserver.NotifyProviderListReady( aError );
       
   765     iWs.Close();
       
   766     // Set error flag if error occurred.
       
   767     if ( aError )
       
   768         {
       
   769         iErrorDownloading = ETrue;
       
   770         }
       
   771     }           
       
   772 
       
   773 // ---------------------------------------------------------------------------
       
   774 // CAcpController::NotifyHttpEvent
       
   775 // From MAcpHttpHandlerObserver.
       
   776 // ---------------------------------------------------------------------------
       
   777 //
       
   778 void CAcpController::NotifyHttpEvent( TInt aEvent )
       
   779     {
       
   780     ACPLOG2( "CAcpController::NotifyHttpEvent begin (%d)", aEvent );
       
   781 
       
   782     TInt error( KErrNone ); // For all operations
       
   783 
       
   784     switch ( aEvent )
       
   785         {
       
   786         // To ignore the following
       
   787         case THTTPEvent::EGotResponseHeaders:
       
   788             break;
       
   789         case THTTPEvent::EGotResponseBodyData:
       
   790             break;
       
   791         case THTTPEvent::EResponseComplete:
       
   792             break;
       
   793         // Request succeeded
       
   794         case THTTPEvent::ESucceeded:
       
   795             {
       
   796             switch ( iType )
       
   797                 {
       
   798                 // XML file for parsing the provider specific data.
       
   799                 case EXml:
       
   800                     {
       
   801                     // Xml data has arrived from server successfully.
       
   802 
       
   803                     // First parse memory root path.
       
   804                     TParse parse;
       
   805                     parse.Set( PathInfo::PhoneMemoryRootPath(), NULL, NULL );
       
   806                     TFileName xmlFile( parse.Drive() );
       
   807                     // Then append file path to memory root.
       
   808                     xmlFile.Append( KXmlFile );
       
   809                     iFilename.Copy( xmlFile );
       
   810                     TRAP( error, CreateFileFromDataL( iFilename ) );
       
   811 
       
   812                     if ( KErrNone == error )
       
   813                         {
       
   814                         TRAP( error, iParser->StartParsingL( iFilename ) );
       
   815                         }
       
   816                     }
       
   817                     break;
       
   818                 // SIS file for installing the provider data on phone.
       
   819                 case ESis:
       
   820                     {
       
   821                     // SIS data has arrived from server successfully.
       
   822 
       
   823                     // First parse memory root path.
       
   824                     TParse parse;
       
   825                     parse.Set( PathInfo::PhoneMemoryRootPath(), NULL, NULL );
       
   826                     TFileName sisFile( parse.Drive() );
       
   827                     // Then append file path to memory root.
       
   828                     sisFile.Append( KSisFile );
       
   829                     iFilename.Copy( sisFile );
       
   830 
       
   831                     TRAP( error, iWs.CommitL() );
       
   832                     iWs.Close();
       
   833                     if ( KErrNone == error )
       
   834                         {
       
   835                         // Set sis installation flag and notify 
       
   836                         // sis downloading completed.
       
   837                         iSisInstallation = ETrue;
       
   838                         iObserver.NotifyDownloadingSISCompleted( iFilename );           
       
   839                         }
       
   840                     }
       
   841                     break;
       
   842                 // Icon file of the provider for displaying own icon on the
       
   843                 // provider list view.
       
   844                 case EIcon:
       
   845                     {
       
   846                     const TInt index = ActiveIndex();
       
   847                     if ( index < iProviders.Count() && index >= 0 )
       
   848                         {
       
   849                         CAcpProvider& provider = *iProviders[index];
       
   850                         TPtrC8 mimeType = provider.ContentData();
       
   851                         TRAP( error,  iImageHandler->StartToDecodeL( 
       
   852                             *iData, mimeType ) );
       
   853                         }
       
   854                     }
       
   855                     break;
       
   856                 // Action file received successfully.
       
   857                 case EAction:
       
   858                     {
       
   859                     TRAP( error, HandleActionFileL() );
       
   860                     ACPLOG2( " - HandleActionFileL error %d", error );
       
   861                     iObserver.NotifyDownloadingCompleted( error );
       
   862                     }
       
   863                     break;
       
   864                 }
       
   865             }            
       
   866             break;
       
   867         case THTTPEvent::EFailed:
       
   868             {
       
   869             ACPLOG( "CAcpController::NotifyHttpEvent:HTTP request failed!" );
       
   870             iWs.Close();
       
   871             iObserver.NotifyProviderListReady( KErrGeneral );
       
   872             
       
   873             // Set error flag.
       
   874             iErrorDownloading = ETrue;
       
   875             }            
       
   876             break;
       
   877         // Ignore all unspecified events.
       
   878         default:
       
   879             {
       
   880             ACPLOG( "CAcpController::NotifyHttpEvent:Unknown HTTP request!" );
       
   881             }
       
   882             break;
       
   883         }
       
   884     }
       
   885 
       
   886 // ---------------------------------------------------------------------------
       
   887 // CAcpController::NotifyBodyReceived
       
   888 // From MAcpHttpHandlerObserver.
       
   889 // ---------------------------------------------------------------------------
       
   890 //
       
   891 void CAcpController::NotifyBodyReceived( const TDesC8& aBodyData )
       
   892     {
       
   893     ACPLOG( "CAcpController::NotifyBodyReceived begin" );
       
   894     ACPLOG2( " - length of received data: %d", aBodyData.Length() );
       
   895 
       
   896     // For SIS files we use RFileWriteStream for storing data.
       
   897     if ( 0 == iFilename.Compare( KSisFile() ) )
       
   898         {
       
   899         TRAP_IGNORE( iWs.WriteL( aBodyData ) );
       
   900         ACPLOG( "CAcpController::NotifyBodyReceived end" );   
       
   901         return;
       
   902         }
       
   903 
       
   904     // Data should exist before going to copy the data received from network.
       
   905     if ( iData && aBodyData.Length() > 0 )
       
   906         {
       
   907         TPtr8 dataPtr( iData->Des() );
       
   908         TRAPD( error, iData = iData->ReAllocL(
       
   909             iData->Length() + aBodyData.Length() ) );
       
   910 
       
   911         if ( KErrNone == error )
       
   912             {
       
   913             // Get old descriptor data into the pointer.
       
   914             dataPtr.Set( iData->Des() );
       
   915             // Copy the new data at the end of the descriptor.
       
   916             dataPtr.Append( aBodyData );
       
   917             }
       
   918         }
       
   919 
       
   920     ACPLOG( "CAcpController::NotifyBodyReceived end" );   
       
   921     }           
       
   922 
       
   923 // ---------------------------------------------------------------------------
       
   924 // CAcpController::NotifyContentTypeReceived
       
   925 // From MAcpHttpHandlerObserver.
       
   926 // ---------------------------------------------------------------------------
       
   927 //
       
   928 void CAcpController::NotifyContentTypeReceived( const TDesC8& aContentData )
       
   929     {
       
   930     ACPLOG( "CAcpController::NotifyContentTypeReceived begin" );
       
   931 
       
   932     TInt index = ActiveIndex();
       
   933 
       
   934     if ( index >= 0 && index < iProviders.Count() )
       
   935         {
       
   936         CAcpProvider& provider = *iProviders[index];
       
   937         // Set the new content type of the received file
       
   938         // into the provider content 
       
   939         TRAP_IGNORE( provider.SetContentDataL( aContentData ) );
       
   940         }
       
   941 
       
   942     ACPLOG( "CAcpController::NotifyContentTypeReceived end" );
       
   943     }
       
   944 
       
   945 // ---------------------------------------------------------------------------
       
   946 // CAcpController::NotifySessionIdReceivedL
       
   947 // From MAcpHttpHandlerObserver.
       
   948 // ---------------------------------------------------------------------------
       
   949 //
       
   950 void CAcpController::NotifySessionIdReceivedL( const TDesC8& aSessionId )
       
   951     {
       
   952     delete iSessionId;
       
   953     iSessionId = NULL;
       
   954     iSessionId = aSessionId.AllocL();
       
   955     }
       
   956 
       
   957 // ---------------------------------------------------------------------------
       
   958 // CAcpController::NotifyImageCompletion
       
   959 // From class MImageHandlerObserver.
       
   960 // An asynchronous notify has occurred related to a icon 
       
   961 // file downloading for the provider.
       
   962 // ---------------------------------------------------------------------------
       
   963 //
       
   964 void CAcpController::NotifyImageCompletion( TInt aErr )
       
   965     {
       
   966     ACPLOG2( "CAcpController::NotifyImageCompletion begin status=%d", aErr );
       
   967 
       
   968     const TInt providerCount = iProviders.Count();
       
   969 
       
   970     if ( KErrNone == aErr )
       
   971         {
       
   972         // Gets an index activated.
       
   973         TInt index = ActiveIndex();
       
   974 
       
   975         // Checks the index validity.
       
   976         if ( index < providerCount )
       
   977             {
       
   978             // Gets a provider.
       
   979             CAcpProvider& provider = *iProviders[index];
       
   980 
       
   981             // Gets a bitmap already converted.
       
   982             CFbsBitmap* bitmap = iImageHandler->GetBitmap();
       
   983             CFbsBitmap* mask = iImageHandler->GetMask();
       
   984 
       
   985             if ( bitmap && mask )
       
   986                 {
       
   987                 // Set the bitmap to the provider.
       
   988                 TRAP_IGNORE( provider.SetBitmapL( bitmap, mask ) );
       
   989                 }
       
   990 
       
   991             // Handle next index if the providers list end wasn't reached.
       
   992             index++;
       
   993             SaveActiveIndex( index );
       
   994             if ( index < providerCount )
       
   995                 {
       
   996                 TPtrC8 iconUrl;
       
   997                 
       
   998                 // Find the next index with an icon URL.
       
   999                 index = NextIndexWithIconUrl( iconUrl );
       
  1000 
       
  1001                 if ( KErrNotFound != index )
       
  1002                     {
       
  1003                     // Delete data buffer.
       
  1004                     TPtr8 dataPtr( iData->Des() );
       
  1005                     dataPtr.Delete( 0, iData->Length() );
       
  1006                     
       
  1007                     // Downloading the icon file from network.
       
  1008                     TRAPD( err, FetchIconFileFromNetworkL( iconUrl ) );
       
  1009                     if ( KErrNone != err )
       
  1010                         {
       
  1011                         iObserver.NotifyProviderListReady( err );
       
  1012                         }
       
  1013                     }
       
  1014                 else // No more icons to load, notify the observer.
       
  1015                     {
       
  1016                     iObserver.NotifyProviderListReady( KErrNone );
       
  1017                     }
       
  1018                 }
       
  1019             else
       
  1020                 {
       
  1021                 // Tell the client that the provider list is now ready
       
  1022                 // for displaying on the screen.
       
  1023                 iObserver.NotifyProviderListReady( KErrNone );
       
  1024                 }
       
  1025             }
       
  1026         }
       
  1027     else
       
  1028         {
       
  1029         // An error notify to the client.
       
  1030         iObserver.NotifyProviderListReady( aErr );
       
  1031         }
       
  1032     ACPLOG( "CAcpController::NotifyImageCompletion end" );
       
  1033     }
       
  1034 
       
  1035 // ---------------------------------------------------------------------------
       
  1036 // CAcpController::ProfileRegistryEventOccurred
       
  1037 // From MSIPProfileRegistryObserver.
       
  1038 // SIP profile information event.
       
  1039 // ---------------------------------------------------------------------------
       
  1040 //
       
  1041 void CAcpController::ProfileRegistryEventOccurred( 
       
  1042     TUint32 /*aSIPProfileId*/, 
       
  1043     TEvent aEvent )
       
  1044     {
       
  1045     ACPLOG2( "CAcpController::ProfileRegistryEventOccurred: %d", aEvent );
       
  1046 
       
  1047     switch ( aEvent )
       
  1048         {
       
  1049         case EProfileCreated:
       
  1050             {
       
  1051             // Notify settings saved if not sis-installation
       
  1052             if ( !iSisInstallation )
       
  1053                 {
       
  1054                 iObserver.NotifySettingsSaved();
       
  1055                 }           
       
  1056             }
       
  1057             break;
       
  1058             
       
  1059         default:
       
  1060             break;
       
  1061         }
       
  1062     }
       
  1063 
       
  1064 // ---------------------------------------------------------------------------
       
  1065 // CAcpController::ProfileRegistryErrorOccurred
       
  1066 // From MSIPProfileRegistryObserver.
       
  1067 // An asynchronous error has occurred related to SIP profile.
       
  1068 // ---------------------------------------------------------------------------
       
  1069 //
       
  1070 void CAcpController::ProfileRegistryErrorOccurred(
       
  1071     TUint32 /*aSIPProfileId*/,
       
  1072     TInt aError )
       
  1073     {
       
  1074     ACPLOG2( "CAcpController::ProfileRegistryErrorOccurred: %d", aError );
       
  1075     //To remove warning: variable / argument 'aError' is not used in function
       
  1076     aError = aError;
       
  1077     }
       
  1078 
       
  1079 
       
  1080 // End of file.