apengine/apeng/src/VpnApEngine.cpp
changeset 0 5a93021fdf25
child 8 2e6c4614c58e
equal deleted inserted replaced
-1:000000000000 0:5a93021fdf25
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Declaration of the CVpnApEngine class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include    "VpnApEngine.h"
       
    22 #include    "VpnApItem.h"
       
    23 #include    "ApEngineLogger.h"
       
    24 #include    "ApEngineConsts.h"
       
    25 #include    "ApEngineCommons.h"
       
    26 
       
    27 #include    <ApDataHandler.h>
       
    28 #include    <ApAccessPointItem.h>
       
    29 #include    <ApUtils.h>
       
    30 
       
    31 #include    <txtetext.h>
       
    32 
       
    33 #include    <vpnapi.h>
       
    34 #include    <vpnapidefs.h>
       
    35 
       
    36 
       
    37 // CONSTANTS
       
    38 _LIT( KVpnVirtualBearerName, "vpnbearer" );
       
    39 _LIT( KVpnVirtualBearerAgent, "vpnconnagt.agt" );
       
    40 _LIT( KVpnVirtualBearerNif, "tunnelnif" );
       
    41 _LIT( KDefProxyProtocolName, "http" );                      // required !
       
    42 _LIT( KDefWapGatewayIpAddress, "0.0.0.0" );
       
    43 
       
    44 
       
    45 
       
    46 // constatnts needed for the virtualbearer table
       
    47 const TInt KNoTimeout = -1;
       
    48 const TUint32 KLastSessionTimeout = 3;
       
    49 
       
    50 
       
    51 // ================= MEMBER FUNCTIONS =======================
       
    52 // ---------------------------------------------------------
       
    53 // CVpnApEngine::NewLC
       
    54 // ---------------------------------------------------------
       
    55 //
       
    56 EXPORT_C CVpnApEngine* CVpnApEngine::NewLC( CCommsDatabase* aDb )
       
    57     {
       
    58     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::NewL" ) ) );
       
    59 
       
    60     CVpnApEngine* self = new ( ELeave ) CVpnApEngine;
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aDb );
       
    63 
       
    64     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::NewL" ) ) );
       
    65     
       
    66     return self;
       
    67     }
       
    68 
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CVpnApEngine::~CVpnApEngine
       
    72 // ---------------------------------------------------------
       
    73 //
       
    74 EXPORT_C CVpnApEngine::~CVpnApEngine()
       
    75     {
       
    76     if ( iOwnDb )
       
    77         {
       
    78         delete iDb;
       
    79         }
       
    80 
       
    81     CLOG( ( EApItem, 0, _L( "<-> CVpnApEngine::~CVpnApEngine" ) ) );
       
    82     }
       
    83 
       
    84         
       
    85 // ---------------------------------------------------------
       
    86 // CVpnApEngine::ConstructL
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 EXPORT_C void CVpnApEngine::ConstructL( CCommsDatabase* aDb )
       
    90     {
       
    91     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::ConstructL" ) ) );
       
    92 
       
    93     if ( aDb )
       
    94         {
       
    95         iDb = aDb;
       
    96         iOwnDb = EFalse;
       
    97         }
       
    98     else
       
    99         {
       
   100         iDb = CCommsDatabase::NewL();
       
   101         iOwnDb = ETrue;
       
   102         }
       
   103 
       
   104     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::ConstructL" ) ) );
       
   105     }
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // CVpnApEngine::CVpnApEngine
       
   110 // ---------------------------------------------------------
       
   111 //
       
   112 EXPORT_C CVpnApEngine::CVpnApEngine()
       
   113 :iOwnDb( EFalse )
       
   114     {
       
   115     CLOG( ( EApItem, 0, _L( "<-> CVpnApEngine::CVpnApEngine" ) ) );
       
   116 
       
   117     }
       
   118 
       
   119 
       
   120 
       
   121 // ---------------------------------------------------------
       
   122 // CVpnApEngine::CreateVpnApL
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 EXPORT_C TUint32 CVpnApEngine::CreateVpnApL( CVpnApItem& aVpnApItem )
       
   126     {
       
   127     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::CreateVpnApL" ) ) );
       
   128 
       
   129     TUint32 retval( 0 );
       
   130 
       
   131     // Start  a transaction
       
   132     TBool ownTransaction = ApCommons::StartPushedTransactionLC( *iDb );
       
   133     
       
   134     retval = DoCreateVpnApL( ETrue, aVpnApItem );
       
   135 
       
   136     if ( ownTransaction )
       
   137         {
       
   138         ApCommons::CommitTransaction( *iDb );
       
   139         CleanupStack::Pop(); // RollbackTransactionOnLeave
       
   140         }
       
   141 
       
   142 
       
   143 
       
   144     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::CreateVpnApL" ) ) );
       
   145 
       
   146     return retval;
       
   147     }
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 
       
   153 // ---------------------------------------------------------
       
   154 // CVpnApEngine::CreateVpnL
       
   155 // ---------------------------------------------------------
       
   156 //
       
   157 EXPORT_C TUint32 CVpnApEngine::CreateVpnApL(  TDesC& aName, 
       
   158                                               TUint32 aIapId, 
       
   159                                               TDesC& aPolicy, 
       
   160                                               TDesC* aProxyServerAddress, 
       
   161                                               TUint32 aProxyPort )
       
   162     {
       
   163     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::CreateVpnApL..." ) ) );
       
   164     TUint32 retval( 0 );
       
   165 
       
   166     CVpnApItem* item = CVpnApItem::NewLC();
       
   167 
       
   168     item->WriteTextL( EApVpnWapAccessPointName, aName );
       
   169     item->WriteUint( EApVpnRealIapID, aIapId );
       
   170     item->WriteTextL( EApVpnPolicyId, aPolicy );
       
   171     item->WriteLongTextL( EApVpnProxyServerAddress, *aProxyServerAddress );
       
   172     item->WriteUint( EApVpnProxyPortNumber, aProxyPort );
       
   173     
       
   174     
       
   175     RVpnServ ps;
       
   176     User::LeaveIfError( ps.Connect() );
       
   177     CleanupClosePushL<RVpnServ>( ps );
       
   178     
       
   179     TVpnPolicyDetails det;
       
   180 
       
   181     TVpnPolicyName pName( aPolicy );
       
   182 
       
   183     TInt err = ps.GetPolicyDetails( pName, det );
       
   184    
       
   185     // When the policy is deleted (err!=0):
       
   186     // have this leave, in UI display a note about needing to be reconfigured
       
   187     // & set it & policyID to none to look like there is NO policy
       
   188     // which will force the reconfiguration if in the UI
       
   189     User::LeaveIfError( err );
       
   190 
       
   191     item->WriteTextL( EApVpnPolicy, det.iName );
       
   192 
       
   193     CreateVpnApL( *item );
       
   194 
       
   195     CleanupStack::PopAndDestroy(); // ps
       
   196 
       
   197     CleanupStack::PopAndDestroy( item );
       
   198 
       
   199     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::CreateVpnApL..." ) ) );
       
   200 
       
   201     return retval;
       
   202     }
       
   203 
       
   204 
       
   205 
       
   206 
       
   207 // ---------------------------------------------------------
       
   208 // CVpnApEngine::DeleteVpnL
       
   209 // ---------------------------------------------------------
       
   210 //
       
   211 EXPORT_C void CVpnApEngine::DeleteVpnL( TUint32 aVpnApId )
       
   212     {
       
   213     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::DeleteVpnL" ) ) );
       
   214 
       
   215     TBool ownTransaction = ApCommons::StartPushedTransactionLC( *iDb );
       
   216     DoDeleteVpnL( aVpnApId );
       
   217     if ( ownTransaction )
       
   218         {
       
   219         ApCommons::CommitTransaction( *iDb );
       
   220         CleanupStack::Pop(); // RollbackTransactionOnLeave
       
   221         }
       
   222 
       
   223     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::DeleteVpnL" ) ) );
       
   224     }
       
   225 
       
   226 
       
   227 
       
   228 
       
   229 
       
   230 
       
   231 // ---------------------------------------------------------
       
   232 // CVpnApEngine::VpnDataL
       
   233 // ---------------------------------------------------------
       
   234 //
       
   235 EXPORT_C void CVpnApEngine::VpnDataL( TUint32 aUid, 
       
   236                                       CVpnApItem& aVpnApItem )
       
   237     {
       
   238     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::VpnDataL" ) ) );
       
   239 
       
   240     ReadVpnWapBearerDataL( aUid, aVpnApItem );
       
   241     ReadVpnWapApDataL( aVpnApItem );
       
   242     ReadVpnIapRecordL( aVpnApItem );
       
   243     ReadNetworkDataL( aVpnApItem );
       
   244     ReadVpnDataL( aVpnApItem );
       
   245     ReadProxyDataL( aVpnApItem );
       
   246 
       
   247     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::VpnDataL" ) ) );
       
   248     }
       
   249 
       
   250 
       
   251 
       
   252 
       
   253 // ---------------------------------------------------------
       
   254 // CVpnApEngine::UpdateVpnDataL
       
   255 // ---------------------------------------------------------
       
   256 //
       
   257 EXPORT_C void CVpnApEngine::UpdateVpnDataL( CVpnApItem& aVpnApItem )
       
   258     {
       
   259     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::UpdateVpnDataL" ) ) );
       
   260 
       
   261     // Start  a transaction
       
   262     TBool ownTransaction = ApCommons::StartPushedTransactionLC( *iDb );
       
   263     
       
   264     // ignore return value as it is used only when creating new
       
   265     DoCreateVpnApL( EFalse, aVpnApItem );
       
   266 
       
   267     if ( ownTransaction )
       
   268         {
       
   269         ApCommons::CommitTransaction( *iDb );
       
   270         CleanupStack::Pop(); // RollbackTransactionOnLeave
       
   271         }
       
   272 
       
   273     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::UpdateVpnDataL" ) ) );
       
   274     }
       
   275 
       
   276 
       
   277 
       
   278 
       
   279 // ---------------------------------------------------------
       
   280 // CVpnApEngine::IsVpnApL()
       
   281 // ---------------------------------------------------------
       
   282 //
       
   283 EXPORT_C TBool CVpnApEngine::IsVpnApL( const TUint32 aUid )
       
   284     {
       
   285     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::IsVpnApL" ) ) );
       
   286 
       
   287     TBool retval( EFalse );
       
   288     // Start  a transaction
       
   289     TBool ownTransaction = ApCommons::StartPushedTransactionLC( *iDb );
       
   290     
       
   291     CCommsDbTableView* wapt;
       
   292     wapt = iDb->OpenViewMatchingUintLC( TPtrC(WAP_IP_BEARER),
       
   293                                        TPtrC(WAP_ACCESS_POINT_ID), aUid );
       
   294     User::LeaveIfError( wapt->GotoFirstRecord() );
       
   295 
       
   296     TUint32 iapid( 0 );
       
   297     ApCommons::ReadUintL( wapt, TPtrC(WAP_IAP), iapid );
       
   298     
       
   299 
       
   300     
       
   301     CCommsDbTableView* iapt;
       
   302     iapt = iDb->OpenViewMatchingUintLC( TPtrC(IAP),
       
   303                                        TPtrC(COMMDB_ID), iapid );
       
   304     User::LeaveIfError( iapt->GotoFirstRecord() );
       
   305 
       
   306     HBufC* iapServiceType = ApCommons::ReadText16ValueLC
       
   307                                     (
       
   308                                     iapt,
       
   309                                     TPtrC(IAP_SERVICE_TYPE)
       
   310                                     );
       
   311 
       
   312     retval = ( iapServiceType->Compare( TPtrC(VPN_SERVICE) ) == 0 );
       
   313 
       
   314     CleanupStack::PopAndDestroy( iapServiceType ); // iapServiceType 
       
   315 
       
   316     CleanupStack::PopAndDestroy( iapt ); // iapt
       
   317     
       
   318     CleanupStack::PopAndDestroy( wapt ); // wapt
       
   319 
       
   320     if ( ownTransaction )
       
   321         {
       
   322         ApCommons::CommitTransaction( *iDb );
       
   323         CleanupStack::Pop(); // RollbackTransactionOnLeave
       
   324         }
       
   325 
       
   326     CLOG( ( EApItem, 1, _L( "<- CVpnApEngine::IsVpnApL" ) ) );
       
   327 
       
   328     return retval;
       
   329     }
       
   330 
       
   331 
       
   332 
       
   333 // ---------------------------------------------------------
       
   334 // CVpnApEngine::Cvt()
       
   335 // ---------------------------------------------------------
       
   336 //
       
   337 EXPORT_C TInt CVpnApEngine::Cvt()
       
   338     {
       
   339     return KErrNone;
       
   340     }
       
   341 
       
   342 
       
   343 
       
   344 
       
   345 // ---------------------------------------------------------
       
   346 // CVpnApEngine::DoCreateVpnApL
       
   347 // ---------------------------------------------------------
       
   348 //
       
   349 TUint32 CVpnApEngine::DoCreateVpnApL( TBool aIsNew, CVpnApItem& aVpnApItem )
       
   350     {
       
   351     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::DoCreateVpnApL" ) ) );
       
   352 
       
   353     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   354     // create 1 single virtual bearer if not already present
       
   355     // Create a new VPN access point (WAP, etc. with VPN IAP
       
   356     // Create a new VPN record in VPN table
       
   357     
       
   358     CApUtils* util = CApUtils::NewLC( *iDb );        
       
   359     TBool valid = 
       
   360             util->IsValidNameL( *aVpnApItem.iName, aVpnApItem.iVpnWapId );
       
   361     CleanupStack::PopAndDestroy( util );
       
   362 
       
   363     if ( !valid )
       
   364         {
       
   365         User::Leave( KErrAlreadyExists );
       
   366         }
       
   367 
       
   368     WriteVpnDataL( aIsNew, aVpnApItem );
       
   369     TUint32 virtbearerid = CreateVirtBearerIfNeededL();
       
   370     aVpnApItem.iVirtualBearerID = virtbearerid;
       
   371     WriteNetworkDataL( aIsNew, aVpnApItem );
       
   372     WriteVpnIapRecordL( aIsNew, aVpnApItem );
       
   373  
       
   374     WriteVpnWapApDataL( aIsNew, aVpnApItem );
       
   375     WriteVpnWapBearerDataL( aIsNew, aVpnApItem );
       
   376     if ( aVpnApItem.HasProxy() )
       
   377         {
       
   378         WriteProxyDataL( aIsNew, aVpnApItem );
       
   379         }
       
   380     else
       
   381         {
       
   382         if ( !aIsNew )
       
   383             {
       
   384             CCommsDbTableView* iapt;
       
   385             iapt = iDb->OpenViewMatchingUintLC( TPtrC(IAP), TPtrC(COMMDB_ID),
       
   386                                                 aVpnApItem.iVpnIapId );
       
   387 
       
   388             User::LeaveIfError( iapt->GotoFirstRecord() );
       
   389             HBufC* servicetype = HBufC::NewLC( KCommsDbSvrMaxFieldLength );
       
   390             TPtr servptr( servicetype->Des() );
       
   391             iapt->ReadTextL( TPtrC(IAP_SERVICE_TYPE), servptr );
       
   392 
       
   393             TRAP_IGNORE( RemoveVpnProxyDataL( aVpnApItem.iVpnTableId ) );
       
   394 
       
   395             CleanupStack::PopAndDestroy( servicetype );
       
   396             CleanupStack::PopAndDestroy( iapt );
       
   397             }
       
   398         }
       
   399     
       
   400     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::DoCreateVpnApL" ) ) );
       
   401 
       
   402     return aVpnApItem.iVpnWapId;
       
   403     }
       
   404 
       
   405 
       
   406 // ---------------------------------------------------------
       
   407 // CVpnApEngine::WriteVpnDataL
       
   408 // ---------------------------------------------------------
       
   409 //
       
   410 void CVpnApEngine::WriteVpnDataL( TBool aIsNew, CVpnApItem& aVpnApItem )
       
   411     {
       
   412     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::WriteVpnDataL" ) ) );
       
   413 
       
   414     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   415     CCommsDbTableView* t;
       
   416 
       
   417     if ( aIsNew )
       
   418         {
       
   419         t = iDb->OpenTableLC( TPtrC(VPN_SERVICE) );
       
   420         TInt err = t->InsertRecord( aVpnApItem.iVpnTableId );
       
   421         if ( err )
       
   422             {
       
   423             User::LeaveIfError( err );
       
   424             }
       
   425         }
       
   426     else
       
   427         {
       
   428         t = iDb->OpenViewMatchingUintLC( TPtrC(VPN_SERVICE),
       
   429                                          TPtrC(COMMDB_ID), 
       
   430                                          aVpnApItem.iVpnTableId );
       
   431         User::LeaveIfError( t->GotoFirstRecord() );
       
   432         User::LeaveIfError( t->UpdateRecord() );
       
   433         }
       
   434 
       
   435     t->WriteTextL( TPtrC(COMMDB_NAME), *aVpnApItem.iName );
       
   436     
       
   437     t->WriteTextL( TPtrC(VPN_SERVICE_POLICY), *aVpnApItem.iPolicyId );
       
   438 
       
   439     t->WriteUintL( TPtrC(VPN_SERVICE_IAP), aVpnApItem.iRealIapId );
       
   440 
       
   441     t->WriteUintL( TPtrC(VPN_SERVICE_NETWORKID), aVpnApItem.iHomeNetwork );
       
   442 
       
   443     User::LeaveIfError( t->PutRecordChanges( EFalse, EFalse ) );
       
   444 
       
   445     CleanupStack::PopAndDestroy( t );   // t
       
   446 
       
   447     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::WriteVpnDataL" ) ) );
       
   448 
       
   449     }
       
   450 
       
   451 
       
   452 
       
   453 
       
   454 // ---------------------------------------------------------
       
   455 // CVpnApEngine::CreateVirtBearerIfNeededL
       
   456 // ---------------------------------------------------------
       
   457 //
       
   458 TUint32 CVpnApEngine::CreateVirtBearerIfNeededL()
       
   459     {
       
   460     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::CreateVirtBearerIfNeededL" ) ) );
       
   461     
       
   462     TUint32 retval( 0 );
       
   463 
       
   464     CCommsDbTableView* t = NULL;
       
   465 
       
   466     t = iDb->OpenViewMatchingTextLC( TPtrC(VIRTUAL_BEARER), 
       
   467                                      TPtrC(VIRTUAL_BEARER_AGENT), 
       
   468                                      KVpnVirtualBearerAgent );
       
   469     if ( t->GotoFirstRecord() == KErrNotFound )
       
   470         { // no such yet, create one
       
   471         TInt err = t->InsertRecord( retval );
       
   472         if ( err )
       
   473             {
       
   474             User::LeaveIfError( err );
       
   475             }        
       
   476         t->WriteTextL( TPtrC(COMMDB_NAME), KVpnVirtualBearerName );
       
   477         t->WriteTextL( TPtrC(VIRTUAL_BEARER_AGENT), KVpnVirtualBearerAgent );
       
   478         t->WriteTextL( TPtrC(VIRTUAL_BEARER_NIF), KVpnVirtualBearerNif );
       
   479 
       
   480         t->WriteUintL( TPtrC(LAST_SOCKET_ACTIVITY_TIMEOUT), 
       
   481                        (TUint32)KNoTimeout ); 
       
   482         t->WriteUintL( TPtrC(LAST_SESSION_CLOSED_TIMEOUT), 
       
   483                        KLastSessionTimeout ); 
       
   484         t->WriteUintL( TPtrC(LAST_SOCKET_CLOSED_TIMEOUT), 
       
   485                        (TUint32)KNoTimeout ); 
       
   486 
       
   487         User::LeaveIfError( t->PutRecordChanges( EFalse, EFalse ) );
       
   488 
       
   489         }
       
   490     else
       
   491         { // there is one, use it
       
   492         t->ReadUintL( TPtrC(COMMDB_ID), retval );
       
   493         }
       
   494 
       
   495     CleanupStack::PopAndDestroy( t );   // t
       
   496     
       
   497     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::CreateVirtBearerIfNeededL" ) ) );
       
   498 
       
   499     return retval;
       
   500     }
       
   501 
       
   502 
       
   503 
       
   504 // ---------------------------------------------------------
       
   505 // CVpnApEngine::WriteVpnIapRecordL
       
   506 // ---------------------------------------------------------
       
   507 //
       
   508 TUint32 CVpnApEngine::WriteVpnIapRecordL( TBool aIsNew, 
       
   509                                           CVpnApItem& aVpnApItem )
       
   510     {
       
   511     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::WriteVpnIapRecordL" ) ) );
       
   512 
       
   513     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   514     CCommsDbTableView* t;
       
   515 
       
   516     if ( aIsNew )
       
   517         {
       
   518         t = iDb->OpenTableLC( TPtrC(IAP) );
       
   519         TInt err = t->InsertRecord( aVpnApItem.iVpnIapId );
       
   520         if ( err )
       
   521             {
       
   522             User::LeaveIfError( err );
       
   523             }
       
   524         }
       
   525     else
       
   526         {
       
   527         t = iDb->OpenViewMatchingUintLC( TPtrC(IAP),
       
   528                                          TPtrC(COMMDB_ID), 
       
   529                                          aVpnApItem.iVpnIapId );
       
   530         User::LeaveIfError( t->GotoFirstRecord() );
       
   531         User::LeaveIfError( t->UpdateRecord() );
       
   532         }
       
   533 
       
   534     t->WriteTextL( TPtrC(COMMDB_NAME), *aVpnApItem.iName );
       
   535 
       
   536     t->WriteUintL( TPtrC(IAP_SERVICE), aVpnApItem.iVpnTableId );
       
   537     t->WriteTextL( TPtrC(IAP_SERVICE_TYPE), TPtrC(VPN_SERVICE) );
       
   538 
       
   539     // hardcoded...
       
   540     t->WriteTextL( TPtrC(IAP_BEARER_TYPE), TPtrC(VIRTUAL_BEARER) );
       
   541     t->WriteUintL( TPtrC(IAP_BEARER), aVpnApItem.iVirtualBearerID );
       
   542 
       
   543     t->WriteUintL( TPtrC(IAP_NETWORK), aVpnApItem.iVpnIapNetwork );
       
   544 
       
   545     // Network weighting hardcoded to 0!
       
   546     t->WriteUintL( TPtrC(IAP_NETWORK_WEIGHTING), 0 );
       
   547     
       
   548     TUint32 loc = GetLocationIdL();
       
   549     t->WriteUintL( TPtrC(IAP_LOCATION), loc );
       
   550 
       
   551     User::LeaveIfError( t->PutRecordChanges( EFalse, EFalse ) );
       
   552 
       
   553     CleanupStack::PopAndDestroy( t );   // t
       
   554 
       
   555    
       
   556     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::WriteVpnIapRecordL" ) ) );
       
   557 
       
   558     return aVpnApItem.iVpnIapId;
       
   559     }
       
   560 
       
   561 
       
   562 
       
   563 
       
   564 // ---------------------------------------------------------
       
   565 // CVpnApEngine::GetLocationIdL
       
   566 // ---------------------------------------------------------
       
   567 //
       
   568 TUint32 CVpnApEngine::GetLocationIdL()
       
   569     {
       
   570     // agreed that the location record having the name 'Mobile' is used
       
   571     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::GetLocationIdL" ) ) );
       
   572 
       
   573     CCommsDbTableView* loc;
       
   574     loc = iDb->OpenViewMatchingTextLC( TPtrC(LOCATION), 
       
   575                                        TPtrC(COMMDB_NAME), 
       
   576                                        KLocation );
       
   577     TInt err = loc->GotoFirstRecord();
       
   578 
       
   579     User::LeaveIfError( err );
       
   580     TUint32 id( 0 );
       
   581     loc->ReadUintL( TPtrC(COMMDB_ID), id );
       
   582 
       
   583     CleanupStack::PopAndDestroy( loc ); // loc
       
   584 
       
   585     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::GetLocationIdL" ) ) );
       
   586     return id;    
       
   587     }
       
   588 
       
   589 
       
   590 
       
   591 // ---------------------------------------------------------
       
   592 // CVpnApEngine::WriteVpnWapApDataL
       
   593 // ---------------------------------------------------------
       
   594 //
       
   595 void CVpnApEngine::WriteVpnWapApDataL( TBool aIsNew, CVpnApItem& aVpnApItem )
       
   596     {
       
   597     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::WriteVpnWapApDataL" ) ) );
       
   598 
       
   599     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   600     CCommsDbTableView* wapt;
       
   601 
       
   602     if ( aIsNew )
       
   603         {
       
   604         wapt = iDb->OpenTableLC( TPtrC(WAP_ACCESS_POINT) );
       
   605         TInt err = wapt->InsertRecord( aVpnApItem.iVpnWapId );
       
   606         if ( err )
       
   607             {
       
   608             User::LeaveIfError( err );
       
   609             }
       
   610         // CommDB's back-link makes returned UID value unusable,
       
   611         // variable needed just to be able to make the call...
       
   612         }
       
   613     else
       
   614         {
       
   615         wapt = iDb->OpenViewMatchingUintLC( TPtrC(WAP_ACCESS_POINT),
       
   616                                             TPtrC(COMMDB_ID), 
       
   617                                             aVpnApItem.iVpnWapId );
       
   618         User::LeaveIfError( wapt->GotoFirstRecord() );
       
   619         User::LeaveIfError( wapt->UpdateRecord() );
       
   620         }
       
   621 
       
   622     // it is IP_BEARER
       
   623     wapt->WriteTextL( TPtrC(WAP_CURRENT_BEARER), TPtrC(WAP_IP_BEARER) );
       
   624     wapt->WriteTextL( TPtrC(COMMDB_NAME), *aVpnApItem.iName );
       
   625 
       
   626     wapt->WriteLongTextL( TPtrC(WAP_START_PAGE), KEmpty );
       
   627 
       
   628     User::LeaveIfError( wapt->PutRecordChanges( EFalse, EFalse ) );
       
   629 
       
   630     CleanupStack::PopAndDestroy( wapt );   // wapt
       
   631 
       
   632     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::WriteVpnWapApDataL" ) ) );
       
   633     }
       
   634 
       
   635 
       
   636 
       
   637 // ---------------------------------------------------------
       
   638 // CVpnApEngine::WriteWapBearerDataL
       
   639 // ---------------------------------------------------------
       
   640 //
       
   641 void CVpnApEngine::WriteVpnWapBearerDataL( TBool aIsNew, 
       
   642                                            CVpnApItem& aVpnApItem )
       
   643     {
       
   644     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::WriteWapBearerDataL" ) ) );
       
   645 
       
   646     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   647     CCommsDbTableView* bearert;
       
   648     if ( aIsNew )
       
   649         {
       
   650         bearert = iDb->OpenTableLC( TPtrC(WAP_IP_BEARER) );
       
   651         // CommDB's back-link makes returned UID value unusable,
       
   652         // variable needed just to be able to make the call...
       
   653         TUint32 alma;
       
   654         User::LeaveIfError( bearert->InsertRecord( alma ) );
       
   655         }
       
   656     else
       
   657         {
       
   658         bearert = iDb->OpenViewMatchingUintLC( TPtrC(WAP_IP_BEARER),
       
   659                                                TPtrC(WAP_ACCESS_POINT_ID),
       
   660                                                aVpnApItem.iVpnWapId );
       
   661         User::LeaveIfError( bearert->GotoFirstRecord() );
       
   662         User::LeaveIfError( bearert->UpdateRecord() );
       
   663         }
       
   664     bearert->WriteTextL( TPtrC(WAP_GATEWAY_ADDRESS), KDefWapGatewayIpAddress );
       
   665 
       
   666 /*
       
   667     bearert->WriteUintL( TPtrC(WAP_WSP_OPTION), 
       
   668                          aApItem.iIsConnectionTypeContinuous );
       
   669     bearert->WriteBoolL( TPtrC(WAP_SECURITY), aApItem.iIsWTLSSecurityOn );
       
   670 */
       
   671     bearert->WriteUintL( TPtrC(WAP_ACCESS_POINT_ID), aVpnApItem.iVpnWapId );
       
   672 
       
   673     bearert->WriteUintL( TPtrC(WAP_IAP), aVpnApItem.iVpnIapId );        
       
   674     
       
   675     // Hardcoded to 0
       
   676     bearert->WriteUintL( TPtrC(WAP_PROXY_PORT), 0 );
       
   677 
       
   678     User::LeaveIfError( bearert->PutRecordChanges( EFalse, EFalse ) );
       
   679 
       
   680     CleanupStack::PopAndDestroy( bearert );   // bearert
       
   681 
       
   682     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::WriteWapBearerDataL" ) ) );
       
   683     }
       
   684 
       
   685 
       
   686 
       
   687 
       
   688 // ---------------------------------------------------------
       
   689 // CVpnApEngine::ReadVpnWapBearerDataL
       
   690 // ---------------------------------------------------------
       
   691 //
       
   692 void CVpnApEngine::ReadVpnWapBearerDataL( TUint32 aUid, 
       
   693                                           CVpnApItem& aVpnApItem )
       
   694     {
       
   695     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::ReadVpnWapBearerDataL" ) ) );
       
   696 
       
   697     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   698     CCommsDbTableView* bearert;
       
   699     bearert = iDb->OpenViewMatchingUintLC( TPtrC(WAP_IP_BEARER),
       
   700                                            TPtrC(WAP_ACCESS_POINT_ID),
       
   701                                            aUid );
       
   702     User::LeaveIfError( bearert->GotoFirstRecord() );
       
   703     aVpnApItem.iVpnWapId = aUid;
       
   704     
       
   705     // for vpn, it is not used....
       
   706     // bearert->WriteTextL( TPtrC(WAP_GATEWAY_ADDRESS), KEmpty );
       
   707 
       
   708 /*
       
   709     bearert->WriteUintL( TPtrC(WAP_WSP_OPTION), 
       
   710                          aApItem.iIsConnectionTypeContinuous );
       
   711     bearert->WriteBoolL( TPtrC(WAP_SECURITY), aApItem.iIsWTLSSecurityOn );
       
   712 */
       
   713 
       
   714     // not neccessary, as we already queried for it and set it...
       
   715     // bearert->ReadUintL( TPtrC(WAP_ACCESS_POINT_ID), aVpnApItem.iVpnWapId );
       
   716 
       
   717     bearert->ReadUintL( TPtrC(WAP_IAP), aVpnApItem.iVpnIapId );        
       
   718     
       
   719     // Hardcoded to 0, not used, skip it 
       
   720     // bearert->WriteUintL( TPtrC(WAP_PROXY_PORT), 0 );
       
   721 
       
   722     CleanupStack::PopAndDestroy( bearert );   // bearert
       
   723 
       
   724     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::ReadVpnWapBearerDataL" ) ) );
       
   725     }
       
   726 
       
   727 
       
   728 
       
   729 
       
   730 // ---------------------------------------------------------
       
   731 // CVpnApEngine::ReadVpnWapApDataL
       
   732 // ---------------------------------------------------------
       
   733 //
       
   734 void CVpnApEngine::ReadVpnWapApDataL( CVpnApItem& aVpnApItem )
       
   735     {
       
   736     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::ReadVpnWapApDataL" ) ) );
       
   737 
       
   738     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   739     CCommsDbTableView* wapt;
       
   740 
       
   741     wapt = iDb->OpenViewMatchingUintLC( TPtrC(WAP_ACCESS_POINT),
       
   742                                         TPtrC(COMMDB_ID), 
       
   743                                         aVpnApItem.iVpnWapId );
       
   744     User::LeaveIfError( wapt->GotoFirstRecord() );
       
   745 
       
   746     // it is IP_BEARER, but as hardcoded, skip it...
       
   747     // wapt->ReadTextL( TPtrC(WAP_CURRENT_BEARER), TPtrC(WAP_IP_BEARER) );
       
   748 
       
   749     HBufC* buf = NULL;
       
   750     buf = ApCommons::ReadText16ValueLC( wapt, TPtrC(COMMDB_NAME) );
       
   751     aVpnApItem.WriteTextL( EApVpnWapAccessPointName, *buf );
       
   752     CleanupStack::PopAndDestroy( buf ); // buf
       
   753 
       
   754     // HARDCODED, skip it
       
   755     //wapt->WriteLongTextL( TPtrC(WAP_START_PAGE), KEmpty );
       
   756 
       
   757     CleanupStack::PopAndDestroy( wapt );   // wapt
       
   758 
       
   759     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::ReadVpnWapApDataL" ) ) );
       
   760     }
       
   761 
       
   762 
       
   763 
       
   764 // ---------------------------------------------------------
       
   765 // CVpnApEngine::ReadVpnIapRecordL
       
   766 // ---------------------------------------------------------
       
   767 //
       
   768 void CVpnApEngine::ReadVpnIapRecordL( CVpnApItem& aVpnApItem )
       
   769     {
       
   770     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::ReadVpnIapRecordL" ) ) );
       
   771 
       
   772     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   773     CCommsDbTableView* t;
       
   774 
       
   775     t = iDb->OpenViewMatchingUintLC( TPtrC(IAP),
       
   776                                      TPtrC(COMMDB_ID), 
       
   777                                      aVpnApItem.iVpnIapId );
       
   778     User::LeaveIfError( t->GotoFirstRecord() );
       
   779 
       
   780     // Name is already set from WAP, skip it
       
   781     // t->WriteTextL( TPtrC(COMMDB_NAME), *aVpnApItem.iName );
       
   782 
       
   783     t->ReadUintL( TPtrC(IAP_SERVICE), aVpnApItem.iVpnTableId );
       
   784     
       
   785     // Hardcoded table name, not used, skip it
       
   786     // t->WriteTextL( TPtrC(IAP_SERVICE_TYPE), TPtrC(VPN_SERVICE) );
       
   787 
       
   788     // hardcoded..., not used, skip it
       
   789     //t->WriteTextL( TPtrC(IAP_BEARER_TYPE), TPtrC(VIRTUAL_BEARER) );
       
   790 
       
   791     t->ReadUintL( TPtrC(IAP_BEARER), aVpnApItem.iVirtualBearerID );
       
   792 
       
   793     t->ReadUintL( TPtrC(IAP_NETWORK), aVpnApItem.iHomeNetwork );
       
   794 
       
   795     // Network weighting hardcoded to 0!, not used, skip it
       
   796     //t->WriteUintL( TPtrC(IAP_NETWORK_WEIGHTING), 0 );
       
   797     
       
   798     // as location is only written, skip it
       
   799     /*
       
   800     TUint32 loc = GetLocationIdL();
       
   801     t->WriteUintL( TPtrC(IAP_LOCATION), loc );
       
   802     */
       
   803 
       
   804     CleanupStack::PopAndDestroy( t );   // t
       
   805    
       
   806     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::ReadVpnIapRecordL" ) ) );
       
   807     }
       
   808 
       
   809 
       
   810 
       
   811 // ---------------------------------------------------------
       
   812 // CVpnApEngine::ReadVpnDataL
       
   813 // ---------------------------------------------------------
       
   814 //
       
   815 void CVpnApEngine::ReadVpnDataL( CVpnApItem& aVpnApItem )
       
   816     {
       
   817     CLOG( ( EApItem, 0, _L( "-> CVpnApEngine::ReadVpnDataL" ) ) );
       
   818 
       
   819     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
   820     CCommsDbTableView* t;
       
   821 
       
   822     t = iDb->OpenViewMatchingUintLC( TPtrC(VPN_SERVICE),
       
   823                                      TPtrC(COMMDB_ID), 
       
   824                                      aVpnApItem.iVpnTableId );
       
   825     User::LeaveIfError( t->GotoFirstRecord() );
       
   826 
       
   827     // already got from WAP table, skip it here
       
   828     //t->WriteTextL( TPtrC(COMMDB_NAME), *aVpnApItem.iName );
       
   829     
       
   830     HBufC* buf = NULL;
       
   831     buf = ApCommons::ReadText16ValueLC( t, TPtrC(VPN_SERVICE_POLICY) );
       
   832     aVpnApItem.WriteTextL( EApVpnPolicyId, *buf );
       
   833     
       
   834     RVpnServ ps;
       
   835     User::LeaveIfError( ps.Connect() );
       
   836     CleanupClosePushL<RVpnServ>( ps );
       
   837     
       
   838     TVpnPolicyName pName( *buf );
       
   839 
       
   840     TInt policyCount(0);
       
   841     TInt err = ps.EnumeratePolicies(policyCount);
       
   842     if ( !err && policyCount)
       
   843         {                
       
   844         CArrayFixFlat<TVpnPolicyInfo>* policyList;
       
   845         policyList = new (ELeave) CArrayFixFlat<TVpnPolicyInfo>(policyCount);
       
   846         CleanupStack::PushL(policyList);
       
   847 
       
   848         err = ps.GetPolicyInfoList(policyList);
       
   849         if ( err )
       
   850             {
       
   851             aVpnApItem.WriteTextL( EApVpnPolicy, KEmpty );
       
   852             }
       
   853         else
       
   854             {
       
   855             TBool found(EFalse);
       
   856             for (TInt i(0); i<policyList->Count(); ++i)
       
   857                 {
       
   858                 if (pName.Compare((*policyList)[i].iId) == 0 )
       
   859                     {
       
   860                     aVpnApItem.WriteTextL( EApVpnPolicy, 
       
   861                         (*policyList)[i].iName );
       
   862                     found = ETrue;
       
   863                     break;    
       
   864                     }
       
   865                 }
       
   866             if (!found)
       
   867                 {
       
   868     // When the policy is deleted (err!=0):
       
   869     // have this leave, in UI display a note about needing to be reconfigured &
       
   870     // set it & policyID to none to look like there is NO policy
       
   871     // which will force the reconfiguration if in the UI
       
   872     //    User::LeaveIfError( err );
       
   873                 aVpnApItem.WriteTextL( EApVpnPolicy, KEmpty );
       
   874                 }
       
   875             }
       
   876         CleanupStack::PopAndDestroy(policyList);
       
   877         }
       
   878 
       
   879     CleanupStack::PopAndDestroy(); // ps
       
   880 
       
   881     CleanupStack::PopAndDestroy( buf ); // buf
       
   882 
       
   883     t->ReadUintL( TPtrC(VPN_SERVICE_IAP), aVpnApItem.iRealIapId );    
       
   884 
       
   885     t->ReadUintL( TPtrC(VPN_SERVICE_NETWORKID), aVpnApItem.iHomeNetwork );
       
   886 
       
   887     CleanupStack::PopAndDestroy( t );   // t
       
   888 
       
   889     // Get real IAP name !!!!!
       
   890     CCommsDbTableView* iapt;
       
   891 
       
   892     iapt = iDb->OpenViewMatchingUintLC( TPtrC(IAP),
       
   893                                      TPtrC(COMMDB_ID), 
       
   894                                      aVpnApItem.iRealIapId );
       
   895 
       
   896     err = iapt->GotoFirstRecord();
       
   897     if ( err == KErrNotFound ) 
       
   898         {
       
   899         aVpnApItem.WriteTextL( EApVpnRealIapName, KEmpty );
       
   900         aVpnApItem.iVpnRealWapId = 0;
       
   901         }
       
   902     else
       
   903         {        
       
   904         User::LeaveIfError( err );
       
   905         buf = ApCommons::ReadText16ValueLC( iapt, TPtrC(COMMDB_NAME) );
       
   906         aVpnApItem.WriteTextL( EApVpnRealIapName, *buf );
       
   907         CleanupStack::PopAndDestroy( buf ); // buf
       
   908 
       
   909         CApUtils* util = CApUtils::NewLC( *iDb );
       
   910         
       
   911         TUint32 realwapid = util->WapIdFromIapIdL( aVpnApItem.iRealIapId );
       
   912         aVpnApItem.iVpnRealWapId = realwapid;
       
   913 
       
   914         CleanupStack::PopAndDestroy( util );
       
   915 
       
   916         }
       
   917 
       
   918     CleanupStack::PopAndDestroy( iapt );   // t
       
   919 
       
   920     CLOG( ( EApItem, 0, _L( "<- CVpnApEngine::ReadVpnDataL" ) ) );
       
   921     }
       
   922 
       
   923 
       
   924 
       
   925 
       
   926 
       
   927 
       
   928 
       
   929 
       
   930 
       
   931 
       
   932 
       
   933 // ---------------------------------------------------------
       
   934 // CVpnApEngine::WriteProxyDataL
       
   935 // ---------------------------------------------------------
       
   936 //
       
   937 void CVpnApEngine::WriteProxyDataL( TBool aIsNew, 
       
   938                                       const CVpnApItem& aVpnApItem )
       
   939     {
       
   940     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::WriteProxyDataL" ) ) );
       
   941 
       
   942     CCommsDbTableView* proxies;
       
   943     if ( aIsNew )
       
   944         {
       
   945         proxies = iDb->OpenTableLC( TPtrC(PROXIES) );
       
   946         // variable needed just to be able to make the call...
       
   947         TUint32 dummy;
       
   948         User::LeaveIfError( proxies->InsertRecord( dummy ) );
       
   949         }
       
   950     else
       
   951         {
       
   952         proxies = iDb->OpenViewMatchingTextLC( TPtrC(PROXIES),
       
   953                                                TPtrC(PROXY_SERVICE_TYPE),
       
   954                                                TPtrC(VPN_SERVICE) );
       
   955         TInt err = proxies->GotoFirstRecord();
       
   956         if ( err == KErrNotFound )
       
   957             { // originally there were NO proxy, but now it is added
       
   958             // variable needed just to be able to make the call...
       
   959             TUint32 dummy;
       
   960             User::LeaveIfError( proxies->InsertRecord( dummy ) );
       
   961             }
       
   962         else
       
   963             { 
       
   964             // check if it is our proxy...
       
   965             // Add test cases covering this route
       
   966             TUint32 uval;
       
   967             TBool found( EFalse );
       
   968             do
       
   969                 {
       
   970                 // Add test cases covering this route
       
   971                 proxies->ReadUintL( TPtrC(PROXY_ISP), uval );
       
   972                 if ( uval == aVpnApItem.iVpnTableId )
       
   973                     {
       
   974                     found = ETrue;
       
   975                     }
       
   976                 else
       
   977                     {
       
   978                     err = proxies->GotoNextRecord();
       
   979                     }
       
   980                 } while ( (!err) && (!found) );
       
   981             if ( !found )
       
   982                 { // no proxy record, add it now
       
   983                 // variable needed just to be able to make the call...
       
   984                 TUint32 alma;
       
   985                 User::LeaveIfError( proxies->InsertRecord( alma ) );
       
   986                 }
       
   987             else
       
   988                 { // we found our proxy, use it...
       
   989                 User::LeaveIfError( proxies->UpdateRecord() );
       
   990                 }
       
   991             }
       
   992         }
       
   993     proxies->WriteTextL( TPtrC(PROXY_SERVICE_TYPE),
       
   994                          TPtrC(VPN_SERVICE) );
       
   995 
       
   996     proxies->WriteUintL( TPtrC(PROXY_ISP), aVpnApItem.iVpnTableId );
       
   997 
       
   998 
       
   999     proxies->WriteLongTextL( TPtrC(PROXY_SERVER_NAME),
       
  1000                             *aVpnApItem.iProxyServerAddress );
       
  1001 
       
  1002     proxies->WriteTextL( TPtrC(PROXY_PROTOCOL_NAME),
       
  1003                          KDefProxyProtocolName );
       
  1004 
       
  1005     proxies->WriteUintL( TPtrC(PROXY_PORT_NUMBER),
       
  1006                          aVpnApItem.iProxyPort );
       
  1007 
       
  1008     TBool useproxy( EFalse );
       
  1009     if ( ( aVpnApItem.iProxyPort ) 
       
  1010         || ( aVpnApItem.iProxyServerAddress->Compare( KEmpty ) != 0 ) )
       
  1011         {
       
  1012         useproxy = ETrue;
       
  1013         }
       
  1014 
       
  1015     proxies->WriteBoolL( TPtrC(PROXY_USE_PROXY_SERVER), 
       
  1016                          useproxy );
       
  1017 
       
  1018     /*
       
  1019     not used, skipped
       
  1020     proxies->WriteLongTextL( TPtrC(PROXY_EXCEPTIONS),
       
  1021                             *aApItem.iApProxyExceptions );
       
  1022     */
       
  1023     proxies->WriteLongTextL( TPtrC(PROXY_EXCEPTIONS),
       
  1024                             KEmpty );
       
  1025 
       
  1026     User::LeaveIfError( proxies->PutRecordChanges( EFalse, EFalse ) );
       
  1027 
       
  1028     CleanupStack::PopAndDestroy( proxies );   // proxies
       
  1029 
       
  1030     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::WriteProxyDataL" ) ) );
       
  1031     }
       
  1032 
       
  1033 
       
  1034 
       
  1035 // ---------------------------------------------------------
       
  1036 // CVpnApEngine::ReadProxyDataL
       
  1037 // ---------------------------------------------------------
       
  1038 //
       
  1039 void CVpnApEngine::ReadProxyDataL( CVpnApItem& aVpnApItem )
       
  1040     {
       
  1041     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
  1042     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::ReadProxyDataL" ) ) );
       
  1043 
       
  1044     CCommsDbTableView* proxies;    
       
  1045     proxies = iDb->OpenViewMatchingTextLC( TPtrC(PROXIES),
       
  1046                                            TPtrC(PROXY_SERVICE_TYPE),
       
  1047                                            TPtrC(VPN_SERVICE) );
       
  1048     TInt err = proxies->GotoFirstRecord();
       
  1049     if ( err != KErrNotFound )
       
  1050         {
       
  1051         User::LeaveIfError( err );
       
  1052         }
       
  1053     if ( err != KErrNotFound )
       
  1054         {
       
  1055         // check if it is our proxy...
       
  1056         // Add test cases covering this route
       
  1057         TUint32 uval;
       
  1058         TBool found( EFalse );
       
  1059         do
       
  1060             {
       
  1061             // Add test cases covering this route
       
  1062             proxies->ReadUintL( TPtrC(PROXY_ISP), uval );
       
  1063             if ( uval == aVpnApItem.iVpnTableId )
       
  1064                 {
       
  1065                 found = ETrue;
       
  1066                 }
       
  1067             else
       
  1068                 {
       
  1069                 err = proxies->GotoNextRecord();
       
  1070                 }
       
  1071             } while ( (!err) && (!found) );
       
  1072         if ( found )
       
  1073             { // it is our proxy record, use it 
       
  1074             HBufC* buf = ApCommons::ReadLongTextLC( proxies,
       
  1075                                                     TPtrC(PROXY_SERVER_NAME),
       
  1076                                                     err );
       
  1077             if ( err != KErrUnknown )
       
  1078                 {
       
  1079                 aVpnApItem.WriteLongTextL( EApVpnProxyServerAddress, *buf );
       
  1080                 }
       
  1081 
       
  1082             CleanupStack::PopAndDestroy( buf ); // buf
       
  1083 
       
  1084             TUint32 tempint;
       
  1085 
       
  1086             ApCommons::ReadUintL( proxies, TPtrC(PROXY_PORT_NUMBER), tempint );
       
  1087             aVpnApItem.WriteUint( EApVpnProxyPortNumber, tempint );
       
  1088 
       
  1089             /*
       
  1090             buf = ApCommons::ReadLongTextLC( proxies,
       
  1091                                              TPtrC(PROXY_EXCEPTIONS),
       
  1092                                              err );
       
  1093             aVpnApItem.WriteLongTextL( EApProxyExceptions, *buf );
       
  1094             CleanupStack::PopAndDestroy( buf ); // buf
       
  1095             */
       
  1096             }
       
  1097         }
       
  1098     CleanupStack::PopAndDestroy( proxies ); // proxies
       
  1099 
       
  1100     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::ReadProxyDataL" ) ) );
       
  1101     }
       
  1102 
       
  1103 
       
  1104 
       
  1105 
       
  1106 // ---------------------------------------------------------
       
  1107 // CVpnApEngine::RemoveVpnProxyDataL
       
  1108 // ---------------------------------------------------------
       
  1109 //
       
  1110 void CVpnApEngine::RemoveVpnProxyDataL( TUint32 aIspId )
       
  1111     {
       
  1112     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
  1113     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::RemoveProxyDataL" ) ) );
       
  1114 
       
  1115     // Add test cases covering all this functions routes
       
  1116 
       
  1117     CCommsDbTableView* proxies;    
       
  1118     proxies = iDb->OpenViewMatchingTextLC( TPtrC(PROXIES), 
       
  1119                                            TPtrC(PROXY_SERVICE_TYPE),
       
  1120                                            TPtrC(VPN_SERVICE) );
       
  1121     TInt err = proxies->GotoFirstRecord();
       
  1122     if ( err == KErrNone )
       
  1123         { // now get OUR proxy record
       
  1124         TUint32 uval;
       
  1125         TBool found( EFalse );
       
  1126         do
       
  1127             {
       
  1128             // Add test cases covering this route
       
  1129             proxies->ReadUintL( TPtrC(PROXY_ISP), uval );
       
  1130             if ( uval == aIspId )
       
  1131                 {
       
  1132                 found = ETrue;
       
  1133                 }
       
  1134             else
       
  1135                 {
       
  1136                 err = proxies->GotoNextRecord();
       
  1137                 }
       
  1138             } while ( (!err) && (!found) );
       
  1139         if ( found )
       
  1140             {
       
  1141             User::LeaveIfError( proxies->DeleteRecord() );
       
  1142             }
       
  1143         else
       
  1144             { 
       
  1145             // silently ignore KErrNotFound. It is caused by incorrect DB,
       
  1146             // we are 'repairing it' this way.
       
  1147             if ( err != KErrNotFound )
       
  1148                 {
       
  1149                 User::LeaveIfError( err );
       
  1150                 }
       
  1151             }
       
  1152         }
       
  1153     else
       
  1154         {
       
  1155         // silently ignore KErrNotFound. It is caused by incorrect DB,
       
  1156         // we are 'repairing it' this way.
       
  1157         if ( err != KErrNotFound )
       
  1158             {
       
  1159             User::LeaveIfError( err );
       
  1160             }
       
  1161         }
       
  1162     CleanupStack::PopAndDestroy( proxies ); // proxies
       
  1163     
       
  1164     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::RemoveProxyDataL" ) ) );
       
  1165     }
       
  1166 
       
  1167 
       
  1168 
       
  1169 // ---------------------------------------------------------
       
  1170 // CVpnApEngine::DoDeleteVpnL
       
  1171 // ---------------------------------------------------------
       
  1172 //
       
  1173 void CVpnApEngine::DoDeleteVpnL( TUint32 aUid )
       
  1174     {
       
  1175     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::DoDeleteVpnL" ) ) );
       
  1176 
       
  1177     // when deleting, need to delete: 
       
  1178     // homenetwork
       
  1179     // VPN_TABLE record
       
  1180     // IAP's VPN record
       
  1181     // WAP's VPN record
       
  1182     // IP_BEARER's record
       
  1183     // proxy record
       
  1184 
       
  1185     // first make sure that the given AP is not in use
       
  1186     CApUtils* utils = CApUtils::NewLC( *iDb );
       
  1187     if ( utils->IsAPInUseL( aUid ) )
       
  1188         {
       
  1189         User::Leave( KErrInUse );
       
  1190         }
       
  1191     CleanupStack::PopAndDestroy( utils );   // utils
       
  1192 
       
  1193     CCommsDbTableView* wapt;
       
  1194     wapt = iDb->OpenViewMatchingUintLC( TPtrC(WAP_ACCESS_POINT),
       
  1195                                        TPtrC(COMMDB_ID), aUid );
       
  1196     User::LeaveIfError( wapt->GotoFirstRecord() );
       
  1197     HBufC* buf = HBufC::NewLC( KCommsDbSvrMaxFieldLength );
       
  1198     TPtr ptr( buf->Des() );
       
  1199     wapt->ReadTextL( TPtrC(WAP_CURRENT_BEARER),
       
  1200                      ptr ); // see declaration for comments!
       
  1201 
       
  1202     if ( *buf == TPtrC(WAP_IP_BEARER) )
       
  1203         {
       
  1204         // get serviceType
       
  1205         CCommsDbTableView* bearert;
       
  1206         bearert = iDb->OpenViewMatchingUintLC( TPtrC(WAP_IP_BEARER),
       
  1207                                                TPtrC(WAP_ACCESS_POINT_ID),
       
  1208                                                aUid );
       
  1209 
       
  1210         User::LeaveIfError( bearert->GotoFirstRecord() );
       
  1211 
       
  1212         TUint32 ipbearerid;
       
  1213         ApCommons::ReadUintL( bearert, TPtrC(COMMDB_ID), ipbearerid );
       
  1214 
       
  1215         TUint32 iapid;
       
  1216         ApCommons::ReadUintL( bearert, TPtrC(WAP_IAP), iapid );
       
  1217 
       
  1218         CCommsDbTableView* iapt;
       
  1219         iapt = iDb->OpenViewMatchingUintLC( TPtrC(IAP), TPtrC(COMMDB_ID),
       
  1220                                             iapid );
       
  1221 
       
  1222         User::LeaveIfError( iapt->GotoFirstRecord() );
       
  1223         HBufC* servicetype = HBufC::NewLC( KCommsDbSvrMaxFieldLength );
       
  1224         TPtr servptr( servicetype->Des() );
       
  1225         iapt->ReadTextL( TPtrC(IAP_SERVICE_TYPE), servptr );
       
  1226         TUint32 ispid;
       
  1227         iapt->ReadUintL( TPtrC(IAP_SERVICE), ispid );
       
  1228         TUint32 networkid;
       
  1229         iapt->ReadUintL( TPtrC(IAP_NETWORK), networkid );
       
  1230 
       
  1231         RemoveNetworkDataL( networkid );
       
  1232 
       
  1233         if ( *servicetype == TPtrC(VPN_SERVICE) )
       
  1234             {
       
  1235             RemoveRecordL( TPtrC(VPN_SERVICE), ispid );
       
  1236             RemoveRecordL( TPtrC(IAP), iapid );
       
  1237             }
       
  1238         else
       
  1239             { // what to do if none of them???
       
  1240             User::Leave( KErrInvalidBearer );
       
  1241             }
       
  1242         RemoveVpnProxyDataL( ispid );
       
  1243         RemoveRecordL( TPtrC(WAP_IP_BEARER), ipbearerid );
       
  1244         // servicetype, iapt, bearert
       
  1245         CleanupStack::PopAndDestroy( 3, bearert ); 
       
  1246         }
       
  1247     else
       
  1248         { // sms,
       
  1249         User::Leave( KErrInvalidBearerType );
       
  1250         }
       
  1251 
       
  1252     RemoveRecordL( TPtrC(WAP_ACCESS_POINT), aUid );
       
  1253 
       
  1254     CleanupStack::PopAndDestroy( 2, wapt );   // buf, wapt
       
  1255 
       
  1256     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::DoDeleteVpnL" ) ) );
       
  1257     }
       
  1258 
       
  1259 
       
  1260 
       
  1261 // ---------------------------------------------------------
       
  1262 // CVpnApEngine::RemoveRecordL
       
  1263 // ---------------------------------------------------------
       
  1264 //
       
  1265 void CVpnApEngine::RemoveRecordL( const TDesC& aTable, TUint32 aUid )
       
  1266     {
       
  1267     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::RemoveRecordL" ) ) );
       
  1268 
       
  1269     CCommsDbTableView* t = iDb->OpenViewMatchingUintLC( 
       
  1270                                             aTable, 
       
  1271                                             TPtrC(COMMDB_ID), 
       
  1272                                             aUid );
       
  1273     TInt res = t->GotoFirstRecord(); // OK.
       
  1274     if ( res == KErrNone )
       
  1275         { // exists, delete it
       
  1276         User::LeaveIfError( t->DeleteRecord() );
       
  1277         }
       
  1278     else
       
  1279         {
       
  1280         // silently ignore KErrNotFound. It is caused by incorrect DB,
       
  1281         // we are 'repairing it' this way.
       
  1282         if ( res != KErrNotFound )
       
  1283             {
       
  1284             User::Leave( res );
       
  1285             }
       
  1286         }
       
  1287     CleanupStack::PopAndDestroy( t ); // t
       
  1288 
       
  1289     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::RemoveRecordL" ) ) );
       
  1290     }
       
  1291 
       
  1292 
       
  1293 
       
  1294 
       
  1295 
       
  1296 // ---------------------------------------------------------
       
  1297 // CVpnApEngine::WriteNetworkDataL
       
  1298 // ---------------------------------------------------------
       
  1299 //
       
  1300 void CVpnApEngine::WriteNetworkDataL( TBool aIsNew, 
       
  1301                                         CVpnApItem& aVpnApItem )
       
  1302     {
       
  1303     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::WriteNetworkDataL" ) ) );
       
  1304 
       
  1305     if ( aVpnApItem.iVpnIapNetwork == 0 )
       
  1306         {
       
  1307         CCommsDbTableView* networks;
       
  1308         HBufC* buf = HBufC::NewLC( KApEngineMaxSql );
       
  1309         if ( aIsNew )
       
  1310             {
       
  1311             networks = iDb->OpenTableLC( TPtrC(NETWORK) );
       
  1312             TUint32 netId;
       
  1313             User::LeaveIfError( networks->InsertRecord( netId ) );
       
  1314             aVpnApItem.iVpnIapNetwork = netId;
       
  1315             }
       
  1316         else
       
  1317             {
       
  1318             networks = iDb->OpenViewMatchingUintLC( 
       
  1319                                 TPtrC(NETWORK), TPtrC(COMMDB_ID), 
       
  1320                                 aVpnApItem.iVpnIapNetwork );
       
  1321 
       
  1322             TInt err = networks->GotoFirstRecord();
       
  1323             if ( err == KErrNotFound )
       
  1324                 { // originally there were NO network, but now it is added
       
  1325                 TUint32 netId;
       
  1326                 User::LeaveIfError( networks->InsertRecord( netId ) );
       
  1327                 aVpnApItem.iVpnIapNetwork = netId;
       
  1328                 }
       
  1329             else
       
  1330                 {
       
  1331                 User::LeaveIfError( networks->UpdateRecord() );
       
  1332                 }
       
  1333             }
       
  1334         networks->WriteTextL( TPtrC(COMMDB_NAME), *aVpnApItem.iName );
       
  1335 
       
  1336         User::LeaveIfError( networks->PutRecordChanges( EFalse, EFalse ) );
       
  1337 
       
  1338         CleanupStack::PopAndDestroy( networks );   // networks
       
  1339         CleanupStack::PopAndDestroy( buf );   // buf
       
  1340         }
       
  1341     
       
  1342     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::WriteNetworkDataL" ) ) );
       
  1343     }
       
  1344 
       
  1345 
       
  1346 // ---------------------------------------------------------
       
  1347 // CVpnApEngine::ReadNetworkDataL
       
  1348 // ---------------------------------------------------------
       
  1349 //
       
  1350 void CVpnApEngine::ReadNetworkDataL( CVpnApItem& aVpnApItem )
       
  1351     {    
       
  1352     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
  1353     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::ReadNetworkDataL" ) ) );
       
  1354         
       
  1355     CCommsDbTableView* networks;
       
  1356     networks = iDb->OpenViewMatchingUintLC( TPtrC(NETWORK), 
       
  1357                                             TPtrC(COMMDB_ID), 
       
  1358                                             aVpnApItem.iVpnIapNetwork );
       
  1359 
       
  1360     TInt err = networks->GotoFirstRecord();
       
  1361 
       
  1362     if ( err != KErrNotFound )
       
  1363         {
       
  1364         User::LeaveIfError( err );
       
  1365         }
       
  1366     if ( err != KErrNotFound )
       
  1367         {
       
  1368         HBufC* buf = ApCommons::ReadText16ValueLC( networks, 
       
  1369                                                    TPtrC(COMMDB_NAME) );
       
  1370         if ( err != KErrUnknown )
       
  1371             {
       
  1372             aVpnApItem.WriteTextL( EApVpnIapNetworkName, *buf );
       
  1373             }
       
  1374 
       
  1375         CleanupStack::PopAndDestroy( buf ); // buf
       
  1376         }
       
  1377     else
       
  1378         { // not found, set to NONE
       
  1379         aVpnApItem.WriteTextL( EApVpnIapNetworkName, KEmpty );
       
  1380         }
       
  1381     CleanupStack::PopAndDestroy( networks ); // networks
       
  1382     
       
  1383     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::ReadNetworkDataL" ) ) );
       
  1384   }
       
  1385 
       
  1386 
       
  1387 
       
  1388 // ---------------------------------------------------------
       
  1389 // CVpnApEngine::RemoveNetworkDataL
       
  1390 // ---------------------------------------------------------
       
  1391 //
       
  1392 void CVpnApEngine::RemoveNetworkDataL( TUint32 aIspId )
       
  1393     {
       
  1394     // internal function, caller MUST initiate a transaction, WE WILL NOT.
       
  1395     CLOG( ( EHandler, 0, _L( "-> CVpnApEngine::RemoveNetworkDataL" ) ) );
       
  1396 
       
  1397     RemoveRecordL( TPtrC(NETWORK), aIspId );
       
  1398 
       
  1399     CLOG( ( EHandler, 1, _L( "<- CVpnApEngine::RemoveNetworkDataL" ) ) );
       
  1400     }
       
  1401 
       
  1402 //  End of File
       
  1403