bearermanagement/mpm/src/mpmvpntogglewatcher.cpp
changeset 41 bbb64eb3bdee
child 71 9f263f780e41
equal deleted inserted replaced
40:c5b848e6c7d1 41:bbb64eb3bdee
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Listen VPN toggle key changes in central repository.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <centralrepository.h>
       
    19 #include <mpmvpntoggleapi.h>
       
    20 #include "mpmlogger.h"
       
    21 #include "mpmvpntogglewatcher.h"
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // Default constructor
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 CMpmVpnToggleWatcher::CMpmVpnToggleWatcher( MMpmVpnToggleWatcherNotify& aNotify )
       
    28     : CActive( EPriorityStandard ),
       
    29       iNotify( aNotify )
       
    30     {
       
    31     CActiveScheduler::Add( this );    
       
    32     }
       
    33 
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // Creates central repositor object
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 void CMpmVpnToggleWatcher::ConstructL()
       
    40     {
       
    41     TRAPD(err, iRepository = CRepository::NewL( KMpmVpnToggleCenRepUid ));
       
    42     if ( err == KErrNone )
       
    43         {
       
    44         StartL();
       
    45         }
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Creates new object
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CMpmVpnToggleWatcher* CMpmVpnToggleWatcher::NewL( MMpmVpnToggleWatcherNotify& aNotify )
       
    53     {
       
    54     CMpmVpnToggleWatcher* self = new( ELeave ) CMpmVpnToggleWatcher(aNotify);
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop( self );
       
    58     return self;
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Destructor
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CMpmVpnToggleWatcher::~CMpmVpnToggleWatcher()
       
    66     {    
       
    67     Cancel();
       
    68     delete iRepository;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Order notification from changes
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CMpmVpnToggleWatcher::StartL()
       
    76     {
       
    77     MPMLOGSTRING( "CMpmVpnToggleWatcher::StartL" )
       
    78             
       
    79     // Get the initial Connect screen ID from repository.
       
    80     User::LeaveIfError( GetVpnToggleValues() );
       
    81 
       
    82     // Request for notifications.
       
    83     User::LeaveIfError( RequestNotifications() );
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Request notifications.
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 TInt CMpmVpnToggleWatcher::RequestNotifications()
       
    91     {
       
    92     MPMLOGSTRING( "CMpmVpnToggleWatcher::RequestNotifications" )
       
    93 
       
    94     TInt err = iRepository->NotifyRequest( KMpmVpnTogglePreferVpn, iStatus );
       
    95         
       
    96     if ( err == KErrNone )
       
    97         {
       
    98         SetActive();
       
    99         }
       
   100     else
       
   101         {
       
   102         MPMLOGSTRING2( "CMpmVpnToggleWatcher::RequestNotifications, ERROR: %d", err )
       
   103         }
       
   104     return err;
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // Get VPN toggle values.
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 TInt CMpmVpnToggleWatcher::GetVpnToggleValues()
       
   112     {
       
   113     MPMLOGSTRING( "CMpmVpnToggleWatcher::GetVpnToggleValues" )
       
   114             
       
   115     // Get values from central repository    
       
   116     TInt err = iRepository->Get( KMpmVpnTogglePreferVpn, iVpnConnectionPreferred );
       
   117     if ( err != KErrNone )
       
   118         {
       
   119         MPMLOGSTRING2( "CMpmVpnToggleWatcher::GetVpnToggleValues, preferred value, ERROR: %d", err )    
       
   120         return err;
       
   121         }    
       
   122     TInt value(0);    
       
   123     err = iRepository->Get( KMpmVpnToggleIapId, value );
       
   124     if ( err != KErrNone )
       
   125         {
       
   126         MPMLOGSTRING2( "CMpmVpnToggleWatcher::GetVpnToggleValues, IAP Id value, ERROR: %d", err )
       
   127         return err;
       
   128         }    
       
   129     iVpnIapId = value;   
       
   130     err = iRepository->Get( KMpmVpnToggleSnapId, value );
       
   131     if ( err != KErrNone )
       
   132         {
       
   133         MPMLOGSTRING2( "CMpmVpnToggleWatcher::GetVpnToggleValues, SNAP Id value, ERROR: %d", err )
       
   134         return err;
       
   135         }
       
   136     iSnapId = value;        
       
   137     return KErrNone;
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Return VPN toggle value.
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 TBool CMpmVpnToggleWatcher::IsVpnConnectionPreferred() const
       
   145     {
       
   146     return iVpnConnectionPreferred;
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // Return VPN IAP Id.
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 TUint32 CMpmVpnToggleWatcher::VpnIapId() const
       
   154     {
       
   155     return iVpnIapId;
       
   156     }
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // Return SNAP Id.
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 TUint32 CMpmVpnToggleWatcher::SnapId() const
       
   163     {
       
   164     return iSnapId;
       
   165     }
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // Resets VPN toggle values.
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 void CMpmVpnToggleWatcher::ResetVpnToggleValues()
       
   172     {
       
   173     // Cancel listening VPN toggle value change
       
   174     Cancel();
       
   175 
       
   176     // Reset values.
       
   177     iVpnConnectionPreferred = EFalse;
       
   178     iVpnIapId = 0;
       
   179     iSnapId = 0;
       
   180     
       
   181     TInt err = iRepository->Set( KMpmVpnTogglePreferVpn, 0 );
       
   182     if ( err == KErrNone ) 
       
   183         {
       
   184         err = iRepository->Set( KMpmVpnToggleIapId, 0 );
       
   185         }
       
   186     if ( err == KErrNone )
       
   187         {
       
   188         err = iRepository->Set( KMpmVpnToggleSnapId, 0 );
       
   189         }
       
   190     
       
   191     MPMLOGSTRING2( "CMpmVpnToggleWatcher::ResetVpnToggleValues, ERROR: %d", err )
       
   192     
       
   193     // Restart listening VPN toggle value change
       
   194     RequestNotifications();
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // From class CActive.
       
   199 // When there is a change in VPN toggle key value, event is received in here
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CMpmVpnToggleWatcher::RunL()
       
   203     {            
       
   204     if ( iStatus.Int() < KErrNone )
       
   205         {
       
   206         MPMLOGSTRING2("CMpmVpnToggleWatcher::RunL, status: 0x%08X", iStatus.Int())
       
   207         iErrorCounter++;
       
   208         if ( iErrorCounter > KMpmVpnToggleWatcherCenRepErrorThreshold )
       
   209             {
       
   210             MPMLOGSTRING2("Over %d consecutive errors, stopping notifications permanently.",
       
   211                     KMpmVpnToggleWatcherCenRepErrorThreshold)
       
   212             return;
       
   213             }
       
   214         // Else: Error occured but counter not expired. Proceed.
       
   215         RequestNotifications();
       
   216         }
       
   217     else
       
   218         {
       
   219         // Notification is received ok => Reset the counter.
       
   220         iErrorCounter = 0;
       
   221     
       
   222         RequestNotifications();
       
   223         
       
   224         // Get values from central repository
       
   225         GetVpnToggleValues();
       
   226         
       
   227         // Notify values.
       
   228         TRAP_IGNORE( iNotify.SetVpnToggleValuesL( iVpnConnectionPreferred,
       
   229                                                   iVpnIapId,
       
   230                                                   iSnapId ) );        
       
   231         }    
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // From class CActive.
       
   236 // Cancel outstanding request
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CMpmVpnToggleWatcher::DoCancel()
       
   240     {
       
   241     iRepository->NotifyCancel( KMpmVpnTogglePreferVpn );
       
   242     }