connectionmonitoring/connmon/connectionmonitor/src/cellulardatausagekeyupdater.cpp
changeset 2 086aae6fc07e
child 3 f7816ffc66ed
equal deleted inserted replaced
1:40cb640ef159 2:086aae6fc07e
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Keeps KCurrentCellularDataUsage -key 
       
    15 *               in Repository KCRUidCmManager up-to-date based
       
    16 *               on current network registration status and
       
    17 *               iCellularDataUsageHome and iCellularDataUsageVisitor keys.  
       
    18 */
       
    19 
       
    20 #include <commsdat.h>
       
    21 #include <centralrepository.h>
       
    22 #include <cmmanager.h>
       
    23 #include <cmmanagerkeys.h>
       
    24 #include <cmgenconnsettings.h>
       
    25 #include <rconnmon.h>
       
    26 #include <datamobilitycommsdattypes.h>
       
    27 #include <cmpluginbaseeng.h>
       
    28 
       
    29 #include "cellulardatausagekeyupdater.h"
       
    30 #include "ConnMonServ.h"
       
    31 #include "ConnMonIap.h"
       
    32 #include "ConnMonDef.h"
       
    33 #include "log.h"
       
    34 
       
    35 // Repository for CommsDat
       
    36 const TUid KCDCommsRepositoryId = { 0xCCCCCC00 };
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CCellularDataUsageKeyUpdater::NewL
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CCellularDataUsageKeyUpdater* CCellularDataUsageKeyUpdater::NewL( 
       
    43 	    CConnMonServer* aServer )
       
    44     {
       
    45     CCellularDataUsageKeyUpdater* self = 
       
    46              CCellularDataUsageKeyUpdater::NewLC( aServer );
       
    47     CleanupStack::Pop( self );
       
    48     return self;
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CCellularDataUsageKeyUpdater::NewLC
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CCellularDataUsageKeyUpdater* CCellularDataUsageKeyUpdater::NewLC( 
       
    56         CConnMonServer* aServer )
       
    57     {
       
    58     CCellularDataUsageKeyUpdater* self = 
       
    59              new( ELeave ) CCellularDataUsageKeyUpdater( aServer );
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CCellularDataUsageKeyUpdater::~CCellularDataUsageKeyUpdater
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CCellularDataUsageKeyUpdater::~CCellularDataUsageKeyUpdater()
       
    70     {
       
    71     // Cancel outstanding request, if exists
       
    72     Cancel();
       
    73     delete iRepository;
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CCellularDataUsageKeyUpdater::UpdateKeyL
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CCellularDataUsageKeyUpdater::UpdateKeyL( const TInt aRegistration ) const
       
    81     {
       
    82     LOGENTRFN("CCellularDataUsageKeyUpdater::UpdateKeyL()")
       
    83     LOGIT1("CCellularDataUsageKeyUpdater::UpdateKeyL: aRegistration <%d>", 
       
    84             aRegistration)
       
    85                 
       
    86     TCmGenConnSettings occSettings = ReadGenConnSettingsL();
       
    87   
       
    88     TInt value( occSettings.iCellularDataUsageHome );
       
    89         
       
    90     if ( aRegistration == ENetworkRegistrationExtRoamingInternational )
       
    91         {           
       
    92         value = occSettings.iCellularDataUsageVisitor;
       
    93         }
       
    94 
       
    95     CRepository* cmRepository = NULL;
       
    96     
       
    97     TRAPD( err, cmRepository = CRepository::NewL( KCRUidCmManager ) )
       
    98     
       
    99     if ( err == KErrNone )
       
   100         {
       
   101         TInt previous( 0 );
       
   102         TInt err = cmRepository->Get( KCurrentCellularDataUsage, previous );
       
   103            
       
   104         if ( err == KErrNone && ( value != previous ) )
       
   105             {
       
   106             cmRepository->Set( KCurrentCellularDataUsage, value );
       
   107             LOGIT1("KCurrentCellularDataUsage set to <%d>", value)
       
   108             }    
       
   109         delete cmRepository;    
       
   110         }
       
   111     else
       
   112         {
       
   113         LOGIT1("CCRepository::NewL( KCRUidCmManager ) FAILED <%d>", err)
       
   114         }
       
   115 
       
   116     LOGEXITFN("CCellularDataUsageKeyUpdater::UpdateKeyL()")
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CCellularDataUsageKeyUpdater::CCellularDataUsageKeyUpdater
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 CCellularDataUsageKeyUpdater::CCellularDataUsageKeyUpdater( CConnMonServer* aServer )
       
   124         :
       
   125         CActive( EConnMonPriorityNormal ),
       
   126         iServer( aServer )
       
   127     {
       
   128     iRepository = NULL;
       
   129     iErrorCounter = 0;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CCellularDataUsageKeyUpdater::ConstructL
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void CCellularDataUsageKeyUpdater::ConstructL()
       
   137     {
       
   138     iRepository = CRepository::NewL( KCDCommsRepositoryId );
       
   139     
       
   140     // Find out Default connection table id.
       
   141     // It contains iCellularDataUsageHome and iCellularDataUsageVisitor keys.
       
   142     CMDBSession* db = CMDBSession::NewLC( CMDBSession::LatestVersion() );	
       
   143     TRAPD( err, iTableId = CCDDefConnRecord::TableIdL( *db ) )
       
   144     	
       
   145     if ( err )
       
   146         {
       
   147         LOGIT1("ERROR, CCDDefConnRecord::TableIdL() <%d>", err)
       
   148         // Default connection table did not exist. 
       
   149         // Try once more because CMManager might have created it.
       
   150         iTableId = CCDDefConnRecord::TableIdL( *db );          	
       
   151         }
       
   152     CleanupStack::PopAndDestroy( db );
       
   153     	    
       
   154     CActiveScheduler::Add( this );
       
   155     
       
   156     // Initialize later asynchronously and let ConnMonServ continue now
       
   157     SetActive(); 
       
   158     iInitStatus = &iStatus;
       
   159     User::RequestComplete( iInitStatus, KErrNone );
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CCellularDataUsageKeyUpdater::RequestNotifications
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 TInt CCellularDataUsageKeyUpdater::RequestNotifications()
       
   167     {  	    	
       
   168     LOGIT1("Calling iRepository->NotifyRequest() for table 0x%08X", iTableId)
       
   169     TInt err = iRepository->NotifyRequest( iTableId, KCDMaskShowRecordType, iStatus );
       
   170 
       
   171     if ( err == KErrNone )
       
   172         {
       
   173         SetActive();
       
   174         }
       
   175     else
       
   176         {
       
   177         LOGIT1("ERROR, iRepository->NotifyRequest() <%d>", err)
       
   178         }
       
   179 
       
   180     return err;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CCellularDataUsageKeyUpdater::DoCancel
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CCellularDataUsageKeyUpdater::DoCancel()
       
   188     {
       
   189     iRepository->NotifyCancel( iTableId, KCDMaskShowRecordType );
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CCellularDataUsageKeyUpdater::RunL
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 void CCellularDataUsageKeyUpdater::RunL()
       
   197     {
       
   198     LOGIT(".")
       
   199     LOGIT2("RunL: CCellularDataUsageKeyUpdater 0x%08X, 0x%08X", 
       
   200             iTableId, iStatus.Int())
       
   201 
       
   202     if ( iStatus.Int() < KErrNone )
       
   203         {
       
   204         LOGIT1("CCellularDataUsageKeyUpdater::RunL: error <%d>", iStatus.Int())
       
   205         iErrorCounter++;
       
   206         if ( iErrorCounter > KCenRepErrorRetryCount )
       
   207             {
       
   208             LOGIT1("Over %d consecutive errors, stopping notifications permanently", 
       
   209                    KCenRepErrorRetryCount)
       
   210             return;
       
   211             }
       
   212         }
       
   213     else
       
   214         {
       
   215         iErrorCounter = 0;
       
   216         
       
   217         TRAPD( leaveCode, UpdateKeyL() )
       
   218     
       
   219         if ( leaveCode )
       
   220             {
       
   221             LOGIT1("CCellularDataUsageKeyUpdater::RunL: LEAVE <%d>", leaveCode)
       
   222             }
       
   223         }
       
   224 
       
   225     RequestNotifications();
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CCellularDataUsageKeyUpdater::ReadGenConnSettings
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 TCmGenConnSettings CCellularDataUsageKeyUpdater::ReadGenConnSettingsL() const
       
   233     {
       
   234     TCmGenConnSettings cmGenConnSettings;
       
   235     
       
   236     CMDBSession* db = CMDBSession::NewLC( CMDBSession::LatestVersion() );
       
   237     
       
   238     CMDBRecordSet<CCDDefConnRecord>* defConnRecordSet = 
       
   239         new ( ELeave ) CMDBRecordSet<CCDDefConnRecord>( iTableId );
       
   240     CleanupStack::PushL( defConnRecordSet );
       
   241      
       
   242     defConnRecordSet->LoadL( *db );
       
   243 
       
   244     TInt value = QUERY_INT_FIELD( defConnRecordSet->iRecords[0], 
       
   245                  KCDTIdUsageOfWlan );
       
   246     cmGenConnSettings.iUsageOfWlan = TCmUsageOfWlan( value );
       
   247 
       
   248     value = QUERY_INT_FIELD( defConnRecordSet->iRecords[0], 
       
   249                              KCDTIdCellularDataUsageHome );
       
   250     cmGenConnSettings.iCellularDataUsageHome = TCmCellularDataUsage( value );
       
   251     LOGIT1("cmGenConnSettings.iCellularDataUsageHome <%d>", 
       
   252             cmGenConnSettings.iCellularDataUsageHome)
       
   253 
       
   254     value = QUERY_INT_FIELD( defConnRecordSet->iRecords[0], 
       
   255                              KCDTIdCellularDataUsageVisitor );
       
   256     cmGenConnSettings.iCellularDataUsageVisitor = TCmCellularDataUsage( value );
       
   257     LOGIT1("cmGenConnSettings.iCellularDataUsageVisitor <%d>", 
       
   258             cmGenConnSettings.iCellularDataUsageVisitor)
       
   259             
       
   260     CleanupStack::PopAndDestroy( defConnRecordSet );	
       
   261     CleanupStack::PopAndDestroy( db );	
       
   262     
       
   263     return cmGenConnSettings;
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCellularDataUsageKeyUpdater::GetNetworkRegistration
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 TInt CCellularDataUsageKeyUpdater::GetNetworkRegistration( TInt& aRegistration ) const
       
   271     {
       
   272     return iServer->Iap()->GetNetworkRegistration_v2( aRegistration );
       
   273     }
       
   274     
       
   275 // -----------------------------------------------------------------------------
       
   276 // CCellularDataUsageKeyUpdater::UpdateKeyL
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CCellularDataUsageKeyUpdater::UpdateKeyL() const
       
   280     {
       
   281     TInt registration( ENetworkRegistrationExtHomeNetwork );
       
   282     TInt err( KErrNone );
       
   283     	
       
   284 #ifndef __WINSCW__  // emulator does not support cellular network    	
       
   285     err = GetNetworkRegistration( registration );
       
   286 #endif
       
   287     
       
   288     if ( err == KErrNone )
       
   289         {
       
   290         UpdateKeyL( registration );
       
   291         }
       
   292     else
       
   293         {
       
   294         LOGIT1("GetNetworkRegistration failed: <%d>", err );
       
   295         }
       
   296     }
       
   297 
       
   298 // End-of-file