accessoryservices/pluggeddisplay/pluggeddisplayengine/src/centralrepositorywatch.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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:  A class to watch changes of key values in Central Repository.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include    <e32base.h>
       
    21 #include    <e32def.h>
       
    22 #include    <centralrepository.h>
       
    23 
       
    24 
       
    25 #include "centralrepositorywatch.h"
       
    26 #include "trace.h"
       
    27 
       
    28 
       
    29 // ======== LOCAL FUNCTIONS ========
       
    30 
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------
       
    35 // Symbian two-phased constructor
       
    36 // ---------------------------------------------------------
       
    37 //
       
    38 CCentralRepositoryWatch* CCentralRepositoryWatch::NewL(
       
    39         MCentralRepositoryObserver&   aObserver,
       
    40         TUid aRepositoryUid,
       
    41         TUint32 aKeyId )
       
    42     {
       
    43     FUNC_LOG;
       
    44     CCentralRepositoryWatch* self = new ( ELeave ) CCentralRepositoryWatch(
       
    45             aObserver,
       
    46             aRepositoryUid,
       
    47             aKeyId );
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop( self );
       
    51     return self;
       
    52     }
       
    53 
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // Symbian two-phased constructor
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CCentralRepositoryWatch* CCentralRepositoryWatch::NewLC(
       
    60         MCentralRepositoryObserver&   aObserver,
       
    61         TUid aRepositoryUid,
       
    62         TUint32 aKeyId )
       
    63     {
       
    64     FUNC_LOG;
       
    65     CCentralRepositoryWatch* self = new( ELeave ) CCentralRepositoryWatch(
       
    66             aObserver,
       
    67             aRepositoryUid,
       
    68             aKeyId );
       
    69     CleanupStack::PushL( self );
       
    70     self->ConstructL();
       
    71     return self;
       
    72     }
       
    73 
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // Destructor
       
    77 // ---------------------------------------------------------
       
    78 //
       
    79 CCentralRepositoryWatch::~CCentralRepositoryWatch()
       
    80     {
       
    81     FUNC_LOG;
       
    82     Cancel();
       
    83     delete iRepository;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------
       
    87 // SubscribeNotification
       
    88 // ---------------------------------------------------------
       
    89 //
       
    90 void CCentralRepositoryWatch::Watch()
       
    91     {
       
    92     FUNC_LOG;
       
    93     // Subscribe
       
    94     if ( !IsActive() )
       
    95         {
       
    96         iRepository->NotifyRequest( iKey, iStatus );
       
    97         SetActive();        
       
    98         }
       
    99     return;
       
   100     }
       
   101 
       
   102 
       
   103 // ---------------------------------------------------------
       
   104 // Get current value.
       
   105 // ---------------------------------------------------------
       
   106 //
       
   107 TInt CCentralRepositoryWatch::GetCurrentValue( TInt& aValue)
       
   108     {
       
   109     FUNC_LOG;
       
   110     TInt value(0);
       
   111     TInt aError = iRepository->Get(iKey, value); //try to get the value
       
   112     if(KErrNone == aError)
       
   113         {
       
   114         aValue = value;
       
   115         }
       
   116     else 
       
   117         {
       
   118         iObserver.CentRepGetKeyValueFailed( iRepositoryUid, iKey, aError );
       
   119         }
       
   120     INFO_1( "Return aValue 0x%x", aValue );
       
   121     return aError;
       
   122     }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------
       
   126 // From class CActive.
       
   127 // DoCancel
       
   128 // ---------------------------------------------------------
       
   129 //
       
   130 void CCentralRepositoryWatch::DoCancel()
       
   131     {
       
   132     FUNC_LOG;
       
   133     iRepository->NotifyCancel( iKey );
       
   134     }
       
   135 
       
   136 
       
   137 // ---------------------------------------------------------
       
   138 // C++ constructor
       
   139 // ---------------------------------------------------------
       
   140 //
       
   141 CCentralRepositoryWatch::CCentralRepositoryWatch(
       
   142         MCentralRepositoryObserver& aObserver,
       
   143         TUid aRepositoryUid,
       
   144         TUint32 aKeyId )
       
   145     : CActive ( EPriorityStandard),
       
   146     iObserver ( aObserver )
       
   147     {
       
   148     FUNC_LOG;
       
   149     iRepositoryUid = aRepositoryUid;
       
   150     iKey = aKeyId;
       
   151     }
       
   152 
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // ConstructL
       
   156 // ---------------------------------------------------------
       
   157 //
       
   158 void CCentralRepositoryWatch::ConstructL( )
       
   159     {
       
   160     FUNC_LOG;
       
   161     // Connect CenRep
       
   162     iRepository = CRepository::NewL( iRepositoryUid );
       
   163     // Check that key is valid
       
   164     TInt aCurrentValue;
       
   165     TInt errorCode = GetCurrentValue( aCurrentValue );
       
   166     if ( KErrNone != errorCode )
       
   167         {
       
   168         User::Leave( errorCode );
       
   169         }
       
   170     CActiveScheduler::Add( this );
       
   171     return;
       
   172    }
       
   173 
       
   174 // ---------------------------------------------------------
       
   175 // From class CActive.
       
   176 // RunL
       
   177 // ---------------------------------------------------------
       
   178 //
       
   179 void CCentralRepositoryWatch::RunL()
       
   180     {
       
   181     FUNC_LOG;
       
   182 
       
   183     // Get the value
       
   184     TInt aCurrentValue = 0;
       
   185     
       
   186     // Re-subscribe before notifying The Master
       
   187     iRepository->NotifyRequest( iKey, iStatus );
       
   188     SetActive();
       
   189     
       
   190     TInt errorCode = GetCurrentValue( aCurrentValue );
       
   191     if ( KErrNone == errorCode )
       
   192         {
       
   193         // Notify requester
       
   194         iObserver.CentRepKeyChanged(
       
   195                 iRepositoryUid,
       
   196                 iKey );
       
   197         }
       
   198     else
       
   199         {
       
   200         INFO_1( "RRepsitory::Get()failed aError %d", errorCode );
       
   201         iObserver.CentRepGetKeyValueFailed( iRepositoryUid, iKey, errorCode );
       
   202         }
       
   203     }
       
   204 
       
   205 
       
   206 // ---------------------------------------------------------
       
   207 // From class CActive.
       
   208 // RunL
       
   209 // ---------------------------------------------------------
       
   210 //
       
   211 TInt CCentralRepositoryWatch::RunError( TInt aError )
       
   212     {
       
   213     FUNC_LOG;
       
   214     // Avoid Panic in CActiveScheduler
       
   215     INFO_1( "aError %d", aError );
       
   216     INFO("Issuing a new request for key value changes");
       
   217     Watch();
       
   218     aError = KErrNone;
       
   219     return aError;
       
   220     }
       
   221 
       
   222 
       
   223 // ======== GLOBAL FUNCTIONS ========
       
   224