contextframework/cfw/src/cfservices/CFContextSubscriptionImpl.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  CCFContextSubscriptionImpl class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32strm.h>
       
    20 #include <s32mem.h>
       
    21 #include <cfcontextdataobject.h>
       
    22 
       
    23 #include "CFContextSubscriptionImpl.h"
       
    24 #include "CFContextQueryImpl.h"
       
    25 #include "cfcontextsubscriptionlistener.h"
       
    26 #include "CFContextIndicationImpl.h"
       
    27 #include "cfcontextobject.h"
       
    28 #include "cftrace.h"
       
    29 #include "CFContextDataProxy.h"
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 #ifdef _DEBUG
       
    34 // Panic category
       
    35 _LIT( KPanicCat, "CONTEXTSUBS" );
       
    36 
       
    37 // Panic reasons
       
    38 enum TPanicReason
       
    39     {
       
    40     ENoListenerSet
       
    41     };
       
    42     
       
    43 // Local panic function
       
    44 LOCAL_C void Panic( TInt aCode )
       
    45     {
       
    46     User::Panic( KPanicCat, aCode );
       
    47     }
       
    48 
       
    49 #endif
       
    50 
       
    51 // MEMBER FUNCTIONS
       
    52 
       
    53 EXPORT_C CCFContextSubscriptionImpl* CCFContextSubscriptionImpl::NewL()
       
    54     {
       
    55     FUNC_LOG;
       
    56     
       
    57     CCFContextSubscriptionImpl* self = CCFContextSubscriptionImpl::NewLC();
       
    58     CleanupStack::Pop( self );
       
    59     
       
    60     return self;
       
    61     }
       
    62 
       
    63 EXPORT_C CCFContextSubscriptionImpl* CCFContextSubscriptionImpl::NewLC()
       
    64     {
       
    65     FUNC_LOG;
       
    66     
       
    67     CCFContextSubscriptionImpl* self = new( ELeave ) CCFContextSubscriptionImpl;
       
    68     CleanupStack::PushL( self );
       
    69     self->ConstructL();
       
    70 
       
    71     return self;
       
    72     }
       
    73 
       
    74 EXPORT_C CCFContextSubscriptionImpl* CCFContextSubscriptionImpl::NewL(
       
    75 	RReadStream& aStream)
       
    76     {
       
    77     FUNC_LOG;
       
    78     
       
    79     CCFContextSubscriptionImpl* self =
       
    80         CCFContextSubscriptionImpl::NewLC( aStream );
       
    81     CleanupStack::Pop( self );
       
    82     
       
    83     return self;
       
    84     }
       
    85 
       
    86 EXPORT_C CCFContextSubscriptionImpl* CCFContextSubscriptionImpl::NewLC(
       
    87 	RReadStream& aStream )
       
    88     {
       
    89     FUNC_LOG;
       
    90     
       
    91     CCFContextSubscriptionImpl* self = new( ELeave ) CCFContextSubscriptionImpl;
       
    92 	CleanupStack::PushL( self );
       
    93     self->ConstructL( aStream );
       
    94 
       
    95     return self;
       
    96     }
       
    97 
       
    98 CCFContextSubscriptionImpl::~CCFContextSubscriptionImpl()
       
    99     {
       
   100     FUNC_LOG;
       
   101     
       
   102     delete iContextQuery;
       
   103     }
       
   104 
       
   105 CCFContextSubscriptionImpl::CCFContextSubscriptionImpl()
       
   106     {
       
   107     FUNC_LOG;
       
   108     }
       
   109     
       
   110 void CCFContextSubscriptionImpl::ConstructL()
       
   111     {
       
   112     FUNC_LOG;
       
   113     
       
   114     iContextQuery = CCFContextQueryImpl::NewL( KNullDesC, KNullDesC );
       
   115     }
       
   116     
       
   117 void CCFContextSubscriptionImpl::ConstructL( RReadStream& aStream )
       
   118     {
       
   119     FUNC_LOG;
       
   120 
       
   121     ConstructL();
       
   122     InternalizeL( aStream );    
       
   123     }
       
   124     
       
   125 // METHODS
       
   126 
       
   127 //-----------------------------------------------------------------------------
       
   128 // CCFContextSubscriptionImpl::SetContextTypeL
       
   129 //-----------------------------------------------------------------------------
       
   130 //
       
   131 void CCFContextSubscriptionImpl::SetContextTypeL(
       
   132     const TDesC& aContextType )
       
   133     {
       
   134     FUNC_LOG;
       
   135     
       
   136     iContextQuery->SetTypeL( aContextType );
       
   137     }
       
   138 
       
   139 //-----------------------------------------------------------------------------
       
   140 // CCFContextSubscriptionImpl::ContextType
       
   141 //-----------------------------------------------------------------------------
       
   142 //
       
   143 const TDesC& CCFContextSubscriptionImpl::ContextType() const
       
   144     {
       
   145     FUNC_LOG;
       
   146     
       
   147     return iContextQuery->Type();
       
   148     }
       
   149 
       
   150 //-----------------------------------------------------------------------------
       
   151 // CCFContextSubscriptionImpl::SetContextSourceL
       
   152 //-----------------------------------------------------------------------------
       
   153 //
       
   154 void CCFContextSubscriptionImpl::SetContextSourceL(
       
   155     const TDesC& aContextSource )
       
   156     {
       
   157     FUNC_LOG;
       
   158     
       
   159     iContextQuery->SetSourceL( aContextSource );
       
   160     }
       
   161 
       
   162 //-----------------------------------------------------------------------------
       
   163 // CCFContextSubscriptionImpl::ContextSource
       
   164 //-----------------------------------------------------------------------------
       
   165 //
       
   166 const TDesC& CCFContextSubscriptionImpl::ContextSource() const
       
   167     {
       
   168     FUNC_LOG;
       
   169     
       
   170     return iContextQuery->Source();
       
   171     }
       
   172 
       
   173 //-----------------------------------------------------------------------------
       
   174 // CCFContextSubscriptionImpl::SubTypeMatch
       
   175 //-----------------------------------------------------------------------------
       
   176 //
       
   177 TBool CCFContextSubscriptionImpl::SubTypeMatch() const
       
   178 	{
       
   179 	FUNC_LOG;
       
   180 	
       
   181 	return iContextQuery->SubTypeMatch();
       
   182 	}
       
   183 
       
   184 //-----------------------------------------------------------------------------
       
   185 // CCFContextSubscriptionImpl::SetSubTypeMatch
       
   186 //-----------------------------------------------------------------------------
       
   187 //
       
   188 void CCFContextSubscriptionImpl::SetSubTypeMatch( 
       
   189 	TBool aMatchSubTypes )
       
   190 	{
       
   191 	FUNC_LOG;
       
   192 	
       
   193 	iContextQuery->SetSubTypeMatch( aMatchSubTypes );
       
   194 	}
       
   195 
       
   196 //-----------------------------------------------------------------------------
       
   197 // CCFContextSubscriptionImpl::OnlyChangesIndicated
       
   198 //-----------------------------------------------------------------------------
       
   199 //
       
   200 TBool CCFContextSubscriptionImpl::OnlyChangesIndicated() const
       
   201 	{
       
   202 	FUNC_LOG;
       
   203 	
       
   204 	return iOnlyChangesIndicated;
       
   205 	}
       
   206 
       
   207 //-----------------------------------------------------------------------------
       
   208 // CCFContextSubscriptionImpl::SetOnlyChangesIndicated
       
   209 //-----------------------------------------------------------------------------
       
   210 //
       
   211 void CCFContextSubscriptionImpl::SetOnlyChangesIndicated( 
       
   212 	TBool aIndicateChangesOnly )
       
   213 	{
       
   214 	FUNC_LOG;
       
   215 	
       
   216 	iOnlyChangesIndicated = aIndicateChangesOnly;
       
   217 	}
       
   218 
       
   219 //-----------------------------------------------------------------------------
       
   220 // CCFContextSubscriptionImpl::SetOnlyChangesIndicated
       
   221 //-----------------------------------------------------------------------------
       
   222 //
       
   223 void CCFContextSubscriptionImpl::SetDataObjectEnabled( TBool aEnabled )
       
   224     {
       
   225     FUNC_LOG;
       
   226     
       
   227     iDataObjectEnabled = aEnabled;
       
   228     }
       
   229 
       
   230 //-----------------------------------------------------------------------------
       
   231 // CCFContextSubscriptionImpl::DataObjectEnabled
       
   232 //-----------------------------------------------------------------------------
       
   233 //
       
   234 TBool CCFContextSubscriptionImpl::DataObjectEnabled() const
       
   235     {
       
   236     FUNC_LOG;
       
   237     
       
   238     return iDataObjectEnabled;
       
   239     }
       
   240     
       
   241 //-----------------------------------------------------------------------------
       
   242 // CCFContextSubscriptionImpl::ExternalizeL
       
   243 //-----------------------------------------------------------------------------
       
   244 //
       
   245 EXPORT_C void CCFContextSubscriptionImpl::ExternalizeL(
       
   246     RWriteStream& aStream )
       
   247     {
       
   248     FUNC_LOG;
       
   249 
       
   250     // Context query
       
   251     iContextQuery->ExternalizeL( aStream );
       
   252 
       
   253     // Indicate only changes flag
       
   254     aStream.WriteInt16L( iOnlyChangesIndicated );
       
   255     
       
   256     // Enable data object
       
   257     aStream.WriteInt16L( iDataObjectEnabled );
       
   258     
       
   259     // Commit stream
       
   260     aStream.CommitL();
       
   261     }
       
   262 
       
   263 //-----------------------------------------------------------------------------
       
   264 // CCFContextSubscriptionImpl::InternalizeL
       
   265 //-----------------------------------------------------------------------------
       
   266 //
       
   267 EXPORT_C void CCFContextSubscriptionImpl::InternalizeL(
       
   268     RReadStream& aStream)
       
   269     {
       
   270     FUNC_LOG;
       
   271     
       
   272     // Context query
       
   273     iContextQuery->InternalizeL( aStream );
       
   274 
       
   275     // Indicate only changes flag
       
   276     iOnlyChangesIndicated = aStream.ReadInt16L();
       
   277 
       
   278     // Enable data object
       
   279     iDataObjectEnabled = aStream.ReadInt16L();
       
   280     }
       
   281 
       
   282 //-----------------------------------------------------------------------------
       
   283 // CCFContextSubscriptionImpl::Size
       
   284 //-----------------------------------------------------------------------------
       
   285 //
       
   286 EXPORT_C TInt CCFContextSubscriptionImpl::Size() const
       
   287     {
       
   288     FUNC_LOG;
       
   289     
       
   290     TInt size = 0;
       
   291     
       
   292     // Context query
       
   293     size += iContextQuery->Size();
       
   294     
       
   295     // Indicate only changes flag
       
   296     size += sizeof( iOnlyChangesIndicated );
       
   297     
       
   298     // Enable data object flag
       
   299     size += sizeof( iDataObjectEnabled );
       
   300     
       
   301     return size;
       
   302     }
       
   303 
       
   304 //-----------------------------------------------------------------------------
       
   305 // CCFContextSubscriptionImpl::SubscriptionListener
       
   306 //-----------------------------------------------------------------------------
       
   307 //
       
   308 EXPORT_C MCFContextSubscriptionListener& 
       
   309 	CCFContextSubscriptionImpl::SubscriptionListener() const
       
   310     {
       
   311     FUNC_LOG;
       
   312     
       
   313     return *iSubscriptionListener;
       
   314     }
       
   315 
       
   316 //-----------------------------------------------------------------------------
       
   317 // CCFContextSubscriptionImpl::SetSubscriptionListener
       
   318 //-----------------------------------------------------------------------------
       
   319 //
       
   320 EXPORT_C void CCFContextSubscriptionImpl::SetSubscriptionListener(
       
   321     MCFContextSubscriptionListener& aListener )
       
   322     {
       
   323     FUNC_LOG;
       
   324     
       
   325     iSubscriptionListener = &aListener;
       
   326     }
       
   327     
       
   328 //-----------------------------------------------------------------------------
       
   329 // CCFContextSubscriptionImpl::EvaluateContextChangeL
       
   330 //-----------------------------------------------------------------------------
       
   331 //
       
   332 EXPORT_C void CCFContextSubscriptionImpl::EvaluateContextChangeL(
       
   333     const CCFContextObject& aNewContext,
       
   334     const CCFContextObject& /*aOldContext*/,
       
   335     TBool aValueChanged ) const
       
   336     {
       
   337     FUNC_LOG;
       
   338     
       
   339     __ASSERT_DEBUG( iSubscriptionListener, Panic( ENoListenerSet ) );
       
   340 
       
   341     if( NotificationNeeded( aNewContext, aValueChanged ) )    
       
   342         {
       
   343         CCFContextIndicationImpl* indication =
       
   344             CCFContextIndicationImpl::NewLC( KNullUid );
       
   345         indication->SetContextL( aNewContext );
       
   346         iSubscriptionListener->ContextIndicationL( indication );
       
   347         CleanupStack::Pop( indication );
       
   348         }
       
   349     }
       
   350 
       
   351 //-----------------------------------------------------------------------------
       
   352 // CCFContextSubscriptionImpl::EvaluateContextChangeL
       
   353 //-----------------------------------------------------------------------------
       
   354 //
       
   355 EXPORT_C void CCFContextSubscriptionImpl::EvaluateContextChangeL(
       
   356     const CCFContextObject& aNewContext,
       
   357     CCFContextDataObject& aData,
       
   358     TBool aValueChanged ) const
       
   359     {
       
   360     FUNC_LOG;
       
   361 
       
   362     __ASSERT_DEBUG( iSubscriptionListener, Panic( ENoListenerSet ) );
       
   363 
       
   364     if( NotificationNeeded( aNewContext, aValueChanged ) )    
       
   365         {
       
   366         CCFContextIndicationImpl* indication =
       
   367             CCFContextIndicationImpl::NewLC( aData.Uid() );
       
   368         indication->SetContextL( aNewContext );
       
   369         
       
   370         // If data object associated and it is required by the subscriber, add
       
   371         // it in the context indication.
       
   372         if( iDataObjectEnabled )
       
   373             {
       
   374             // Stream data object
       
   375             HBufC8* data = HBufC8::NewLC( aData.Size() );
       
   376             TPtr8 dataPtr( data->Des() );
       
   377             RDesWriteStream writeStream( dataPtr );
       
   378             writeStream.PushL();
       
   379             aData.ExternalizeL( writeStream );
       
   380             writeStream.CommitL();
       
   381             indication->SetDataObject( data );
       
   382             
       
   383             // Clean up
       
   384             CleanupStack::PopAndDestroy( &writeStream );
       
   385             CleanupStack::Pop( data );
       
   386             }
       
   387         iSubscriptionListener->ContextIndicationL( indication );
       
   388         CleanupStack::Pop( indication );
       
   389         }
       
   390     }
       
   391 
       
   392 //-----------------------------------------------------------------------------
       
   393 // CCFContextSubscriptionImpl::IsSame
       
   394 //-----------------------------------------------------------------------------
       
   395 //
       
   396 EXPORT_C TBool CCFContextSubscriptionImpl::IsSame(
       
   397     const CCFContextSubscriptionImpl& aSubscription ) const
       
   398     {
       
   399     FUNC_LOG;
       
   400     
       
   401     // Check if we have the same session
       
   402     if( &aSubscription.SubscriptionListener() != iSubscriptionListener )
       
   403         {
       
   404         return EFalse;
       
   405         }
       
   406 
       
   407     if( CCFContextQueryImpl::IsSame(
       
   408         *iContextQuery,
       
   409         *aSubscription.iContextQuery ) )
       
   410        	{
       
   411        	return ETrue;
       
   412        	}
       
   413 
       
   414     return EFalse;
       
   415     }
       
   416 
       
   417 //-----------------------------------------------------------------------------
       
   418 // CCFContextSubscriptionImpl::Matches
       
   419 //-----------------------------------------------------------------------------
       
   420 //
       
   421 EXPORT_C TBool CCFContextSubscriptionImpl::Matches(
       
   422     const TDesC& aContextType ) const
       
   423     {
       
   424     FUNC_LOG;
       
   425     
       
   426 	return iContextQuery->Type() == aContextType;
       
   427     }
       
   428 
       
   429 //-----------------------------------------------------------------------------
       
   430 // CCFContextSubscriptionImpl::Matches
       
   431 //-----------------------------------------------------------------------------
       
   432 //
       
   433 EXPORT_C TBool CCFContextSubscriptionImpl::Matches( const TDesC& aContextType,
       
   434     const TDesC& aContextSource ) const
       
   435     {
       
   436     FUNC_LOG;
       
   437     
       
   438 	return iContextQuery->Matches( aContextType, aContextSource );
       
   439     }
       
   440 
       
   441 //-----------------------------------------------------------------------------
       
   442 // CCFContextSubscriptionImpl::NotificationNeeded
       
   443 //-----------------------------------------------------------------------------
       
   444 //
       
   445 TBool CCFContextSubscriptionImpl::NotificationNeeded(
       
   446     const CCFContextObject& aNewContext,
       
   447     TBool aValueChanged ) const
       
   448     {
       
   449     FUNC_LOG;
       
   450 
       
   451     TBool need = EFalse;    
       
   452     if( !iOnlyChangesIndicated || ( iOnlyChangesIndicated && aValueChanged ) )
       
   453     	{
       
   454 	    if( iContextQuery->Matches( aNewContext.Type(), aNewContext.Source() ) )
       
   455 	        {
       
   456 	        need = ETrue;
       
   457 	        }
       
   458     	}
       
   459     	
       
   460     return need;
       
   461     }