bearermanagement/mpm/src/mpmofflinewatcher.cpp
branchGCC_SURGE
changeset 49 faa5ef4f80da
parent 39 f10336de0cd6
parent 47 cb7afde124a3
equal deleted inserted replaced
39:f10336de0cd6 49:faa5ef4f80da
     1 /*
       
     2  * Copyright (c) 2008-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: Listens central repository for offline mode changes.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <centralrepository.h>
       
    20 #include <CoreApplicationUIsSDKCRKeys.h>
       
    21 #include <featmgr.h>
       
    22 
       
    23 #include "mpmlogger.h"
       
    24 #include "mpmserver.h"
       
    25 #include "mpmofflinewatcher.h"
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Default C++ constructor.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CMpmOfflineWatcher::CMpmOfflineWatcher( CMPMServer* aServer ) :
       
    32     CActive( EPriorityStandard ), iServer( aServer )
       
    33     {
       
    34     MPMLOGSTRING( "CMpmOfflineWatcher::CMpmOfflineWatcher" )
       
    35 
       
    36     CActiveScheduler::Add( this );
       
    37     }
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // Symbian 2nd phase constructor. Creates a central repository object.
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 void CMpmOfflineWatcher::ConstructL()
       
    44     {
       
    45     MPMLOGSTRING( "CMpmOfflineWatcher::ConstructL" )
       
    46 
       
    47     FeatureManager::InitializeLibL();
       
    48     // If feature isn't supported, then no watching, but return in StartL().
       
    49     iOfflineFeatureSupported = FeatureManager::FeatureSupported( 
       
    50                                KFeatureIdOfflineMode );
       
    51     FeatureManager::UnInitializeLib();
       
    52 
       
    53         
       
    54     iRepository = CRepository::NewL( KCRUidCoreApplicationUIs );
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Creates a new object by calling the two-phased constructor.
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CMpmOfflineWatcher* CMpmOfflineWatcher::NewL( CMPMServer* aServer )
       
    62     {
       
    63     MPMLOGSTRING( "CMpmOfflineWatcher::NewL" )
       
    64 
       
    65     CMpmOfflineWatcher* self = new( ELeave ) CMpmOfflineWatcher( aServer );
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop( self );
       
    69     return self;
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Destructor.
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CMpmOfflineWatcher::~CMpmOfflineWatcher()
       
    77     {
       
    78     MPMLOGSTRING( "CMpmOfflineWatcher::~CMpmOfflineWatcher" )
       
    79 
       
    80     Cancel();
       
    81     delete iRepository;
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Order notification from changes.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 void CMpmOfflineWatcher::StartL()
       
    89     {
       
    90     MPMLOGSTRING( "CMpmOfflineWatcher::StartL" )
       
    91             
       
    92     if ( !iOfflineFeatureSupported )
       
    93         {
       
    94         return;
       
    95         }
       
    96 
       
    97     // Get the initial data usage value from repository.
       
    98     User::LeaveIfError( GetCurrentOfflineValue() );
       
    99     iServer->UpdateOfflineMode( iOfflineMode );
       
   100     
       
   101     // Request notifications.
       
   102     User::LeaveIfError( RequestNotifications() );
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // From class CActive.
       
   107 // Event is received when there is a change in central repository key.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CMpmOfflineWatcher::RunL()
       
   111     {
       
   112     MPMLOGSTRING( "CMpmOfflineWatcher::RunL" )
       
   113 
       
   114     if ( iStatus.Int() < KErrNone )
       
   115         {
       
   116         MPMLOGSTRING2("Status: 0x%08X", iStatus.Int())
       
   117         iErrorCounter++;
       
   118         if ( iErrorCounter > KMpmOfflineWatcherCenRepErrorThreshold )
       
   119             {
       
   120             MPMLOGSTRING2("Over %d consecutive errors, stopping notifications permanently.",
       
   121                     KMpmOfflineWatcherCenRepErrorThreshold)
       
   122             return;
       
   123             }
       
   124         // Else: Error occured but counter not expired. Proceed.
       
   125         }
       
   126     else
       
   127         {
       
   128         // Notification is received ok => Reset the counter.
       
   129         iErrorCounter = 0;
       
   130 
       
   131         // Check if mode has changed (it should).
       
   132         TInt oldMode = iOfflineMode;
       
   133         
       
   134         TInt err = GetCurrentOfflineValue();
       
   135         if ( err == KErrNone && oldMode != iOfflineMode )
       
   136             {
       
   137             iServer->UpdateOfflineMode( iOfflineMode );
       
   138             }
       
   139         }
       
   140     
       
   141     RequestNotifications();
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // From class CActive.
       
   146 // Cancel the outstanding request.
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CMpmOfflineWatcher::DoCancel()
       
   150     {
       
   151     MPMLOGSTRING( "CMpmOfflineWatcher::DoCancel" )
       
   152 
       
   153     iRepository->NotifyCancel( KCoreAppUIsNetworkConnectionAllowed );
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // Request for notifications.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 TInt CMpmOfflineWatcher::RequestNotifications()
       
   161     {
       
   162     MPMLOGSTRING( "CMpmOfflineWatcher::RequestNotifications" )
       
   163 
       
   164     TInt err = iRepository->NotifyRequest( KCoreAppUIsNetworkConnectionAllowed, iStatus );
       
   165         
       
   166     if ( err == KErrNone )
       
   167         {
       
   168         SetActive();
       
   169         }
       
   170     else
       
   171         {
       
   172         // MPM's offline mode watching wouldn't recover... 
       
   173         MPMLOGSTRING2( "CMpmOfflineWatcher::RequestNotifications, ERROR: %d", err )
       
   174         }
       
   175     return err;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // Get the current repository key value.
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 TInt CMpmOfflineWatcher::GetCurrentOfflineValue()
       
   183     {
       
   184     MPMLOGSTRING( "CMpmOfflineWatcher::GetCurrentOfflineValue" )
       
   185 
       
   186     TInt err = iRepository->Get( KCoreAppUIsNetworkConnectionAllowed, iOfflineMode );
       
   187         
       
   188     if ( err != KErrNone )
       
   189         {
       
   190         MPMLOGSTRING2( "CMpmOfflineWatcher::GetCurrentOfflineValue, ERROR: %d", err )
       
   191         }
       
   192     return err;
       
   193     }