os/persistentdata/featuremgmt/featuremgr/src/clientdll/featurenotifier.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2007-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 
    17 
    18 // INCLUDE FILES
    19 #include <featmgr/featurenotifier.h>
    20 #include "featmgrdebug.h"
    21 #include <featmgr/featurecmn.h>
    22 #include "featmgrclient.h"
    23 
    24 // ============================= LOCAL FUNCTIONS ===============================
    25 
    26 // -----------------------------------------------------------------------------
    27 // FindByUid
    28 // Returns Zero if UIDs do match.
    29 // -----------------------------------------------------------------------------
    30 //  
    31 static TInt FindByUid( const TUid* aFeature, const TUid& aItem )
    32     {
    33     if ( aFeature->iUid == aItem.iUid )
    34         {
    35         return 1;
    36         }
    37 
    38     return 0;
    39     }
    40 
    41     
    42 // ============================ MEMBER FUNCTIONS ===============================
    43 
    44 // -----------------------------------------------------------------------------
    45 // CFeatureNotifier::CFeatureNotifier()
    46 // -----------------------------------------------------------------------------
    47 //
    48 CFeatureNotifier::CFeatureNotifier( MFeatureObserver& aObserver ) :
    49     CActive( EPriorityStandard ),
    50     iObserver( aObserver )
    51     {
    52     }
    53 
    54 // -----------------------------------------------------------------------------
    55 // CFeatureNotifier::NewL()
    56 // Two-phased constructor.
    57 // -----------------------------------------------------------------------------
    58 //
    59 EXPORT_C CFeatureNotifier* CFeatureNotifier::NewL( MFeatureObserver& aObserver )
    60     {
    61     CFeatureNotifier* self = new( ELeave ) CFeatureNotifier( aObserver );
    62     CleanupStack::PushL( self );
    63     self->ConstructL();
    64     CleanupStack::Pop( self );
    65     return self;
    66     }
    67     
    68 // -----------------------------------------------------------------------------
    69 // CFeatureNotifier::ConstructL
    70 // -----------------------------------------------------------------------------
    71 //
    72 void CFeatureNotifier::ConstructL()
    73     {
    74     FUNC_LOG
    75 
    76     iFeatMgrClient = new (ELeave) RFeatMgrClient;
    77     // Connect to Feature Manager server
    78     TInt err( iFeatMgrClient->Connect() );
    79     if (err!=KErrNone)
    80       {
    81       delete iFeatMgrClient;
    82       iFeatMgrClient=NULL;
    83       }
    84     User::LeaveIfError(err);
    85 
    86     CActiveScheduler::Add( this );
    87     }
    88 
    89 // -----------------------------------------------------------------------------
    90 // CFeatureNotifier::~CFeatureNotifier()
    91 // -----------------------------------------------------------------------------
    92 //
    93 EXPORT_C CFeatureNotifier::~CFeatureNotifier()
    94     {
    95     FUNC_LOG
    96     
    97     if( iFeatMgrClient )
    98     	{
    99     	iFeatMgrClient->RequestNotifyCancelAll();
   100         iFeatMgrClient->Close();
   101         delete iFeatMgrClient;
   102     	}
   103     Cancel();
   104 
   105     iFeatures.Close();
   106     }
   107 
   108 // -----------------------------------------------------------------------------
   109 // CFeatureNotifier::NotifyRequest(TUid)
   110 // -----------------------------------------------------------------------------
   111 //
   112 EXPORT_C TInt CFeatureNotifier::NotifyRequest( TUid aFeature )
   113     {
   114     if(IsActive())
   115         {
   116         return KErrAlreadyExists;
   117         }
   118 
   119     iFeatures.Reset();
   120     TInt err=iFeatures.Append( aFeature );
   121     if (err!=KErrNone)
   122       {
   123       return err;
   124       }
   125     err = iFeatMgrClient->RequestNotification( iFeatures, iFeatureChanged, iStatus );
   126     if ( err == KErrNone )
   127     	{
   128         SetActive();
   129      	}
   130     else
   131       {
   132       iFeatures.Reset();
   133       }
   134      	    
   135     return err;
   136     }
   137     
   138 // -----------------------------------------------------------------------------
   139 // CFeatureNotifier::NotifyRequest(RFeatureUidArray&)
   140 // -----------------------------------------------------------------------------
   141 //
   142 EXPORT_C TInt CFeatureNotifier::NotifyRequest( RFeatureUidArray& aFeatures )
   143     {
   144     if(IsActive())
   145         {
   146         return KErrAlreadyExists;
   147         }
   148 
   149     iFeatures.Reset();
   150     TInt count = aFeatures.Count();
   151     TInt err=KErrNone;
   152     for(TInt i = 0; i < count; i++ )
   153         {
   154         // Do not append duplicate entries
   155         const TUid& uid( aFeatures[i] );
   156         TInt index = iFeatures.Find( uid, FindByUid );
   157         if( index == KErrNotFound )
   158             {
   159             err=iFeatures.Append( uid );
   160             if (err!=KErrNone)
   161               {
   162               iFeatures.Reset();
   163               return err;
   164               }
   165             }
   166         }
   167 
   168     err = iFeatMgrClient->RequestNotification( iFeatures, iFeatureChanged, iStatus );
   169     if ( err == KErrNone )
   170     	{
   171         SetActive();
   172      	}
   173     else
   174       {
   175       iFeatures.Reset();
   176       }
   177     return err;
   178     }
   179     
   180 // -----------------------------------------------------------------------------
   181 // CFeatureNotifier::NotifyCancel(TUid aFeature)
   182 // -----------------------------------------------------------------------------
   183 //
   184 EXPORT_C TInt CFeatureNotifier::NotifyCancel( TUid aFeature )
   185     {
   186     TInt err( KErrNotFound );
   187     TInt count = iFeatures.Count();
   188     
   189     for(TInt i = 0; i < count; i++ )
   190         {
   191         if( iFeatures[i].iUid == aFeature.iUid )
   192             {
   193             err = iFeatMgrClient->RequestNotifyCancel( aFeature );
   194             
   195             if( err == KErrNone )
   196                 {
   197                 iFeatures.Remove( i );
   198                 // If this is the last feature in the list, then also cancel the Active Object
   199                 if( 0 == iFeatures.Count() )
   200                 	{
   201 	                Cancel();
   202                 	}
   203                 break;
   204                 }
   205             }
   206         }
   207 
   208     return err;
   209     }
   210     
   211 // -----------------------------------------------------------------------------
   212 // CFeatureNotifier::NotifyCancelAll()
   213 // -----------------------------------------------------------------------------
   214 //
   215 EXPORT_C TInt CFeatureNotifier::NotifyCancelAll()
   216     {
   217     iFeatures.Reset();
   218     TInt err = iFeatMgrClient->RequestNotifyCancelAll( );
   219    	Cancel();
   220     
   221     return err;
   222     }
   223 
   224 /**
   225  Gets the number of features in the server's array.
   226  This API is only for internal use and testing purposes.
   227 
   228  @return The number of features on the server for debug builds, otherwise it returns KErrNotSupported in release builds.
   229  @internalComponent
   230 */
   231 EXPORT_C TInt CFeatureNotifier::NumberOfNotifyFeatures()
   232     {
   233 #ifdef EXTENDED_FEATURE_MANAGER_TEST
   234     TInt count = iFeatMgrClient->NumberOfNotifyFeatures();
   235     return count;
   236 #else
   237     return KErrNotSupported;
   238 #endif
   239     }
   240 
   241 /**
   242  Gets the number of heap cells in the thread.
   243  This API is only for internal use and testing purposes.
   244 
   245  @return The number of heap cells for debug build, otherwise it returns KErrNotSupported in release builds.
   246  @internalComponent
   247 */
   248 EXPORT_C TInt CFeatureNotifier::CountAllocCells()
   249     {
   250 #ifdef EXTENDED_FEATURE_MANAGER_TEST
   251     TInt count = iFeatMgrClient->CountAllocCells();
   252     return count;
   253 #else
   254     return KErrNotSupported;
   255 #endif
   256     }
   257 
   258 // -----------------------------------------------------------------------------
   259 // CFeatureNotifier::RunL()
   260 // -----------------------------------------------------------------------------
   261 //
   262 void CFeatureNotifier::RunL( )
   263     {
   264     FUNC_LOG
   265 
   266     TInt status = iStatus.Int();
   267     if( status < 0 )
   268         {
   269         // Got an error
   270         if( status != KErrCancel )
   271             {
   272             iObserver.HandleNotifyError( status );
   273             }
   274         return;
   275         }
   276 
   277     // iStatus >= 0 means it contains the change type
   278     TFeatureChangeType changeType = static_cast<TFeatureChangeType>(status);
   279 
   280 	// If the feature was deleted, remove it from the array of features with notifications requested
   281     if( changeType == EFeatureFeatureDeleted )
   282     	{
   283         TInt index = iFeatures.Find( iFeatureChanged, FindByUid );
   284         if( index != KErrNotFound )
   285             {
   286             iFeatures.Remove( index );
   287             }
   288     	}
   289     // Should we validate whether iFeatureChanged matches 
   290     // to any UID in the array of requested features?
   291 
   292     TFeatureEntry feature( iFeatureChanged );
   293     if( iFeatures.Count() > 0 )
   294         {
   295         // Asynchronously resubscribe notify request
   296         iFeatMgrClient->ReRequestNotification( iFeatureChanged, iStatus );
   297         // Next inform client about changed feature 
   298         // Should we validate whether iFeatureChanged matches 
   299         // to any UID in the array of requested features
   300         iObserver.HandleNotifyChange( changeType, feature );
   301         // Set us active
   302         SetActive();
   303         }
   304     else
   305         {
   306         // All features with notifications requested were deleted,
   307         // so don't need to resubscribe notify request -
   308         // just notify client about the deleted feature
   309         iObserver.HandleNotifyChange( changeType, feature );
   310         }
   311     }
   312 
   313 // -----------------------------------------------------------------------------
   314 // CFeatureNotifier::DoCancel()
   315 // -----------------------------------------------------------------------------
   316 //
   317 void CFeatureNotifier::DoCancel( )
   318     {
   319     }
   320 
   321 // -----------------------------------------------------------------------------
   322 // CFeatureNotifier::RunError()
   323 // -----------------------------------------------------------------------------
   324 //
   325 TInt CFeatureNotifier::RunError( TInt aError )
   326     {
   327     iObserver.HandleNotifyError( aError );
   328 
   329     return KErrNone;
   330     }
   331 
   332 //  End of File