sysstatemgmt/systemstatereferenceplugins/clayer/src/ssmpropertyobserver.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "ssmpropertyobserver.h"
       
    17 
       
    18 #include "ssmdebug.h"
       
    19 
       
    20 /**
       
    21  * Creates a new CSsmProperty Observer.
       
    22  * 
       
    23  * @param aCategory The category of the property to monitor.
       
    24  * @param aKey The key of the property to monitor.
       
    25  *  
       
    26  * @publishedPartner
       
    27  * @released
       
    28  */
       
    29 EXPORT_C CSsmPropertyObserver::CSsmPropertyObserver(TUid aCategory, TUint aKey)
       
    30 : 	CActive(EPriorityNormal),
       
    31 	iCategory(aCategory),
       
    32 	iKey(aKey)
       
    33 	{
       
    34 	CActiveScheduler::Add(this);
       
    35 	} //lint !e1746 Suppress parameter 'aCategory' could be made const reference
       
    36 
       
    37 /**
       
    38  * Releases any resources allocated to this object.
       
    39  *  
       
    40  * @publishedPartner
       
    41  * @released
       
    42  */
       
    43 EXPORT_C CSsmPropertyObserver::~CSsmPropertyObserver()
       
    44 	{
       
    45 	Deque();
       
    46 	if(iProperty.Handle() != 0)
       
    47 		{
       
    48 		iProperty.Cancel();
       
    49 		}
       
    50 	iProperty.Close();
       
    51 	}
       
    52 
       
    53 /**
       
    54  * Initialises this object but does not start responding to changes to the monitored property.
       
    55  *  
       
    56  * @publishedPartner
       
    57  * @released
       
    58  */
       
    59 EXPORT_C void CSsmPropertyObserver::InitializeL()
       
    60 	{
       
    61 	TInt err = iProperty.Attach(iCategory, iKey);
       
    62 	if(KErrNone != err)
       
    63 		{
       
    64 		DEBUGPRINT2(_L("CSsmPropertyObserver failed to attach to property with error: %d"), err);
       
    65 		User::Leave(err);
       
    66 		}
       
    67 	}
       
    68 
       
    69 /**
       
    70  * Starts this observer responding to the monitored property.
       
    71  *  
       
    72  * @publishedPartner
       
    73  * @released
       
    74  */
       
    75 EXPORT_C void CSsmPropertyObserver::StartL()
       
    76 	{
       
    77 	// To start we just call doRunL()
       
    78 	doRunL();
       
    79 	}
       
    80 
       
    81 /**
       
    82  * Releases this object freeing any resources allocated to it.
       
    83  * 
       
    84  * Using this object after calling release will have undefined behaviour.
       
    85  *  
       
    86  * @publishedPartner
       
    87  * @released
       
    88  */
       
    89 EXPORT_C void CSsmPropertyObserver::Release()
       
    90 	{
       
    91 	delete this;
       
    92 	}
       
    93 
       
    94 /**
       
    95  * CActive::RunL implementation to call the observer function and to re-request monitoring.
       
    96  *  
       
    97  * @publishedPartner
       
    98  * @released
       
    99  */
       
   100 EXPORT_C void CSsmPropertyObserver::RunL()
       
   101 	{
       
   102 	if(KErrNone != iStatus.Int())
       
   103 		{
       
   104 		DEBUGPRINT2A("CSsmPropertyObserver RunL received error status: %d", iStatus.Int());
       
   105 		User::Leave(iStatus.Int());
       
   106 		}
       
   107 	doRunL();	
       
   108 	}
       
   109 
       
   110 /**
       
   111  * Internal RunL helper method
       
   112  * 
       
   113  * @internalComponent
       
   114  */
       
   115 void CSsmPropertyObserver::doRunL()
       
   116 	{
       
   117 	TInt value;
       
   118 	// subscribe before we get the value, deciding later if we should cancel
       
   119 	iProperty.Subscribe(iStatus);
       
   120 	
       
   121 	TInt err = iProperty.Get(value);
       
   122 	if(KErrNone != err)
       
   123 		{
       
   124 		DEBUGPRINT2A("CSsmPropertyObserver failed to get property with error: %d", err);
       
   125 		// Cancel the subscription before leaving 
       
   126 		iProperty.Cancel();
       
   127 		User::WaitForRequest(iStatus);
       
   128 		
       
   129 		User::Leave(err);
       
   130 		}
       
   131 	TBool resubscribe = PropertyChangedL(value);
       
   132 	if(resubscribe)
       
   133 		{
       
   134 		// Just set active as we resubscribed before
       
   135 		SetActive();
       
   136 		}
       
   137 	else
       
   138 		{
       
   139 		// Cancel the previous subscription
       
   140 		iProperty.Cancel();
       
   141 		User::WaitForRequest(iStatus);
       
   142 		}
       
   143 	}
       
   144 
       
   145 /**
       
   146  * Cancels monitoring for this property 
       
   147  *  
       
   148  * @publishedPartner
       
   149  * @released
       
   150  */
       
   151 EXPORT_C void CSsmPropertyObserver::DoCancel()
       
   152 	{
       
   153 	iProperty.Cancel();
       
   154 	}
       
   155 
       
   156 /**
       
   157  * CActive::RunError implementation
       
   158  *  
       
   159  * @publishedPartner
       
   160  * @released
       
   161  */
       
   162 EXPORT_C TInt CSsmPropertyObserver::RunError(TInt aError)
       
   163 	{
       
   164 	DEBUGPRINT4A("Property observer for UID: %x Key: %d RunError received error: %d", iCategory.iUid, iKey, aError);
       
   165 	return aError;
       
   166 	}