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