fmradio/fmradioengine/src/fmradiopropertyobserver.cpp
changeset 0 f3d95d9c00ab
equal deleted inserted replaced
-1:000000000000 0:f3d95d9c00ab
       
     1 /*
       
     2 * Copyright (c) 2004-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: Observer for FMRadio property values  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "fmradiopropertyobserver.h"
       
    20 
       
    21 // ============================ MEMBER FUNCTIONS ===============================
       
    22 
       
    23 // -----------------------------------------------------------------------------
       
    24 // C++ default constructor can NOT contain any code, that might leave.
       
    25 // -----------------------------------------------------------------------------
       
    26 //
       
    27 CFMRadioPropertyObserver::CFMRadioPropertyObserver(MFMRadioPropertyChangeObserver& aObserver, const TUid& aCategory, const TUint aKey, const TFMRadioPropertyType aPropertyType)
       
    28     : CActive( CActive::EPriorityStandard ),
       
    29     iObserver( aObserver ),
       
    30     iCategory( aCategory ),
       
    31     iKey( aKey ),
       
    32     iPropertyType( aPropertyType )
       
    33 	{
       
    34 	}
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // Symbian 2nd phase constructor can leave.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 void CFMRadioPropertyObserver::ConstructL()
       
    41 	{
       
    42     switch (iPropertyType)
       
    43         {
       
    44         case EFMRadioPropertyInt:
       
    45 	        {
       
    46 	        break;
       
    47 	        }
       
    48 	    case EFMRadioPropertyByteArray:
       
    49 	        {
       
    50 	        iValueByteArray = HBufC8::NewL( RProperty::KMaxPropertySize );
       
    51 	        break;
       
    52 	        }
       
    53 	    case EFMRadioPropertyText:
       
    54 	        {
       
    55 	        // Max size in bytes, length is size / 2
       
    56 	        iValueText = HBufC::NewL( RProperty::KMaxPropertySize / 2 );
       
    57 	        break;
       
    58 	        }
       
    59 	    default:
       
    60 	        {
       
    61 	        break;
       
    62 	        }
       
    63         }
       
    64 
       
    65     User::LeaveIfError( iProperty.Attach( iCategory, iKey ) );
       
    66     CActiveScheduler::Add( this );
       
    67 	}
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // Two-phased constructor.
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CFMRadioPropertyObserver* CFMRadioPropertyObserver::NewL( MFMRadioPropertyChangeObserver& aObserver,
       
    74                                                           const TUid& aCategory,
       
    75                                                           const TUint aKey,
       
    76                                                           const TFMRadioPropertyType aPropertyType )
       
    77     {
       
    78     CFMRadioPropertyObserver* self = NewLC( aObserver,
       
    79                                             aCategory,
       
    80                                             aKey,
       
    81                                             aPropertyType );
       
    82     CleanupStack::Pop( self );
       
    83     return self;
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // Two-phased constructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CFMRadioPropertyObserver* CFMRadioPropertyObserver::NewLC( MFMRadioPropertyChangeObserver& aObserver,
       
    91 														  const TUid& aCategory,
       
    92 														  const TUint aKey,
       
    93 														  const TFMRadioPropertyType aPropertyType )
       
    94 	{
       
    95     CFMRadioPropertyObserver* self = new( ELeave )CFMRadioPropertyObserver( aObserver,
       
    96     																   		aCategory,
       
    97     																   		aKey,
       
    98     																   		aPropertyType );
       
    99     CleanupStack::PushL( self );
       
   100     self->ConstructL();
       
   101     return self;
       
   102 	}
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // Destructor
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 CFMRadioPropertyObserver::~CFMRadioPropertyObserver()
       
   109 	{
       
   110 	Cancel();
       
   111     iProperty.Close();
       
   112     delete iValueByteArray;
       
   113     delete iValueText;
       
   114 	}
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Subscribes to a property and reads the value, if not already active.
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void CFMRadioPropertyObserver::ActivateL()
       
   121 	{
       
   122     if ( !IsActive() )
       
   123         {
       
   124         RunL();
       
   125         }
       
   126 	}
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CFMRadioPropertyObserver::RunL
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void CFMRadioPropertyObserver::RunL()
       
   133 	{	
       
   134     iProperty.Subscribe( iStatus );
       
   135     SetActive();
       
   136     
       
   137     TInt err(KErrNone);
       
   138     
       
   139     switch (iPropertyType)
       
   140         {
       
   141         case EFMRadioPropertyInt:
       
   142 	        {
       
   143 	        err = iProperty.Get( iValueInt );
       
   144 	        if (!err)
       
   145             	{
       
   146                 iObserver.HandlePropertyChangeL( iCategory, iKey, iValueInt );
       
   147              	}
       
   148 	        break;
       
   149 	        }
       
   150 		case EFMRadioPropertyByteArray:
       
   151 	        {
       
   152 	        TPtr8 ptr8( iValueByteArray->Des() );
       
   153 	        err = iProperty.Get( ptr8 );
       
   154 	        if (!err)
       
   155             	{
       
   156                 iObserver.HandlePropertyChangeL( iCategory, iKey, *iValueByteArray );
       
   157              	}
       
   158 	        break;		        
       
   159 	        }
       
   160 		case EFMRadioPropertyText:
       
   161 	        {
       
   162 	        TPtr ptr( iValueText->Des() );
       
   163 	        err = iProperty.Get( ptr );
       
   164 	        if (!err)
       
   165             	{
       
   166                 iObserver.HandlePropertyChangeL( iCategory, iKey, *iValueText );
       
   167              	}
       
   168 	        break;			        
       
   169 	        }
       
   170 	        
       
   171 	    default:
       
   172 	        {
       
   173 	        break;
       
   174 	        }
       
   175         }
       
   176     
       
   177     if (err)
       
   178     	{
       
   179         iObserver.HandlePropertyChangeErrorL(iCategory, iKey, err);
       
   180      	}
       
   181 	}
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // Cancels an outstanding active request
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CFMRadioPropertyObserver::DoCancel()
       
   188 	{
       
   189     iProperty.Cancel();
       
   190 	}
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // Getter for integer value
       
   194 // -----------------------------------------------------------------------------
       
   195 //	
       
   196 TInt CFMRadioPropertyObserver::ValueInt( TBool aUpdate )
       
   197 	{
       
   198 	if( aUpdate )
       
   199 	    {
       
   200 	    iProperty.Get( iValueInt );
       
   201 	    }
       
   202 	return iValueInt;
       
   203 	}
       
   204 	
       
   205 // -----------------------------------------------------------------------------
       
   206 // Getter for byte array value
       
   207 // -----------------------------------------------------------------------------
       
   208 //	
       
   209 const TDesC8& CFMRadioPropertyObserver::ValueDes8( TBool aUpdate )
       
   210 	{
       
   211    if( aUpdate )
       
   212         {
       
   213     	TPtr8 ptr8( iValueByteArray->Des() );
       
   214     	iProperty.Get( ptr8 );
       
   215         }
       
   216 	return *iValueByteArray;
       
   217 	}
       
   218 	
       
   219 // -----------------------------------------------------------------------------
       
   220 // Getter for text value
       
   221 // -----------------------------------------------------------------------------
       
   222 //	
       
   223 const TDesC& CFMRadioPropertyObserver::ValueDes( TBool aUpdate )
       
   224 	{
       
   225    if( aUpdate )
       
   226         {
       
   227     	TPtr ptr( iValueText->Des() );
       
   228     	iProperty.Get( ptr );
       
   229         }
       
   230 	return *iValueText;
       
   231 	}
       
   232