securitydialogs/lockapp/src/lockapppubsubobserver.cpp
branchRCL_3
changeset 21 09b1ac925e3f
parent 20 63339781d179
child 22 03674e5abf46
equal deleted inserted replaced
20:63339781d179 21:09b1ac925e3f
     1 /*
       
     2 * Copyright (c) 2007 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:  Publish & Subscribe key observer
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "lockapppubsubobserver.h"
       
    20 #include "lockapputils.h"
       
    21 
       
    22 // ---------------------------------------------------------------------------
       
    23 // Two-phased constructor.
       
    24 // ---------------------------------------------------------------------------
       
    25 CLockAppPubSubObserver* CLockAppPubSubObserver::NewL(MLockAppObserverInterface* aObserver,
       
    26         TUid aPubSubUid, TUint32 aKeyId )
       
    27     {
       
    28     CLockAppPubSubObserver* self = new (ELeave) CLockAppPubSubObserver(aObserver, aPubSubUid, aKeyId);
       
    29     CleanupStack::PushL( self );
       
    30     self->ConstructL( );
       
    31     CleanupStack::Pop( self );
       
    32     return self;
       
    33     }
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // C++ default constructor
       
    37 // ---------------------------------------------------------------------------
       
    38 CLockAppPubSubObserver::CLockAppPubSubObserver(MLockAppObserverInterface* aObserver,
       
    39         TUid aPubSubUid, TUint32 aKeyId ) :
       
    40     CActive( 0 ),
       
    41     iObserver( aObserver ),
       
    42     iPubSubUid( aPubSubUid ),
       
    43     iKeyId( aKeyId ),
       
    44     iValue( NULL )
       
    45     {
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor
       
    50 // ---------------------------------------------------------------------------
       
    51 CLockAppPubSubObserver::~CLockAppPubSubObserver( )
       
    52     {
       
    53     Cancel( );
       
    54     iProperty.Close( );
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Symbian OS default constructor
       
    59 // ---------------------------------------------------------------------------
       
    60 void CLockAppPubSubObserver::ConstructL( )
       
    61     {
       
    62     // Add this active object to the scheduler.
       
    63     CActiveScheduler::Add( this );
       
    64     TInt err = iProperty.Attach( iPubSubUid, iKeyId );
       
    65     ERROR_2(err, "CLockAppPubSubObserver::ConstructL - Property(%d,%d) attach", iPubSubUid, iKeyId);
       
    66     User::LeaveIfError( err );
       
    67     Start( );
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // Starts listening
       
    72 // ---------------------------------------------------------------------------
       
    73 TInt CLockAppPubSubObserver::Start( )
       
    74     {
       
    75     if ( !IsActive( ) )
       
    76         {
       
    77         iStatus = KRequestPending;
       
    78         iProperty.Subscribe( iStatus );
       
    79         SetActive( );
       
    80         return KErrNone;
       
    81         }
       
    82     else
       
    83         {
       
    84         return KErrInUse;
       
    85         }
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Stops listening
       
    90 // ---------------------------------------------------------------------------
       
    91 void CLockAppPubSubObserver::Stop( )
       
    92     {
       
    93     if ( IsActive( ) )
       
    94         {
       
    95         Cancel( );
       
    96         iProperty.Cancel( );
       
    97         }
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Gets value of the key from P&S.
       
   102 // ---------------------------------------------------------------------------
       
   103 TInt CLockAppPubSubObserver::GetKeyValue(TInt& aValue )
       
   104     {
       
   105     RDebug::Printf( "%s %s (%u) 1=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, 1 );
       
   106     return iProperty.Get( aValue );
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // Sets a value for the key in P&S.
       
   111 // ---------------------------------------------------------------------------
       
   112 TInt CLockAppPubSubObserver::SetKeyValue(TInt aValue )
       
   113     {
       
   114     RDebug::Printf( "%s %s (%u) aValue=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, aValue );
       
   115     return iProperty.Set( aValue );
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CLockAppPubSubObserver::RunL()
       
   120 // ---------------------------------------------------------------------------
       
   121 void CLockAppPubSubObserver::RunL( )
       
   122     {
       
   123     TInt value( NULL);
       
   124     iProperty.Get( value );
       
   125     if ( iValue != value )
       
   126         {
       
   127         // on value change
       
   128         iValue = value;
       
   129         if ( iObserver )
       
   130             {
       
   131             iObserver->HandlePubSubNotify( iPubSubUid, iKeyId, iValue );
       
   132             }
       
   133         }
       
   134     // re-subscribe to events
       
   135     Start( );
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // Cancels event listening
       
   140 // ---------------------------------------------------------------------------
       
   141 void CLockAppPubSubObserver::DoCancel( )
       
   142     {
       
   143     iProperty.Cancel( );
       
   144     }
       
   145 
       
   146 // End of file