alarmui/src/alarmcontextfwsupport.cpp
changeset 0 f979ecb2b13e
child 30 d68a4b5d5885
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2006-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:   This class interfaces with Context Framework Client API.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // SYSTEM INCLUDE
       
    21 #include <coemain.h>
       
    22 #include <f32file.h>
       
    23 #include <pathinfo.h>
       
    24 
       
    25 #include <cfclient.h>
       
    26 #include <cfcontextobject.h>
       
    27 #include <cfactionsubscription.h>
       
    28 #include <cfcontextsubscription.h>
       
    29 #include <cfcontextindication.h>
       
    30 #include <cfactionindication.h>
       
    31 
       
    32 // USER INCLUDE
       
    33 #include "alarmcontextfwsupport.h"
       
    34 #include "AlmAlarmControl.h"
       
    35 
       
    36 #include "pim_trace.h"
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Symbian constructor.
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CAlarmContextFwSupport* CAlarmContextFwSupport::NewL(CAlmAlarmControl* aAlarmControl)
       
    45     {
       
    46     TRACE_ENTRY_POINT;
       
    47     CAlarmContextFwSupport* self = new( ELeave )CAlarmContextFwSupport( aAlarmControl );
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop( self );
       
    51     TRACE_EXIT_POINT;
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // destructor
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CAlarmContextFwSupport::~CAlarmContextFwSupport()
       
    60     {
       
    61     TRACE_ENTRY_POINT;
       
    62     CloseCFClient();
       
    63     TRACE_EXIT_POINT;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Indicates a change in context for subscribed clients.
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 void CAlarmContextFwSupport::ContextIndicationL(const CCFContextIndication& aChangedContext)
       
    71     {
       
    72     TRACE_ENTRY_POINT;
       
    73 
       
    74 	
       
    75     const CCFContextObject& contextObject = aChangedContext.Context();
       
    76 
       
    77     if( contextObject.Source() == KAlarmUISource() && contextObject.Type() == KAlarmUISourceCommand() )
       
    78         {
       
    79         
       
    80         if( contextObject.Value() == TPtrC( KAlarmUISourceCommandValues[ECommandAlarmStop] ) )
       
    81             {
       
    82             // stop alarm
       
    83             iAlarmControl->SetStopFromContext(ETrue);
       
    84             iAlarmControl->ExternalStopAlarm();
       
    85             }
       
    86         else if( contextObject.Value() == TPtrC( KAlarmUISourceCommandValues[ECommandAlarmSnooze] ) )
       
    87             {
       
    88             // snooze alarm
       
    89             if(iAlarmControl->CanSnooze())
       
    90 	            {
       
    91 	            iAlarmControl->ExternalSnoozeAlarm();
       
    92 	            }
       
    93             }
       
    94         else
       
    95             {
       
    96             // value didn't match
       
    97             }
       
    98         }
       
    99     else
       
   100         {
       
   101         //source didn't match!
       
   102         }
       
   103     TRACE_EXIT_POINT;
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // Indicates that specified action is needed to be executed.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CAlarmContextFwSupport::ActionIndicationL(const CCFActionIndication& aActionToExecute)
       
   111     {
       
   112     TRACE_ENTRY_POINT;
       
   113 
       
   114     if( aActionToExecute.Identifier() == TPtrC( KAlarmUIActions[EActionSnooze] ) )
       
   115         {
       
   116         // snooze the alarm
       
   117 		if(iAlarmControl->CanSnooze())
       
   118         	iAlarmControl->ExternalSnoozeAlarm();
       
   119         }
       
   120     else if( aActionToExecute.Identifier() == TPtrC( KAlarmUIActions[EActionStop] ) )
       
   121         {
       
   122         //Do not stop the alarm internally but force the user to stop the alarm manually.
       
   123         //iAlarmControl->ExternalStopAlarm();
       
   124         }
       
   125     TRACE_EXIT_POINT;
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // Indicates that error has occured.
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CAlarmContextFwSupport::HandleContextFrameworkError( TCFError aError,
       
   133                                                           const TDesC& /*aSource*/,
       
   134                                                           const TDesC& /*aType*/ )
       
   135     {
       
   136     TRACE_ENTRY_POINT;
       
   137     // no implementation
       
   138     ASSERT( !aError );
       
   139     TRACE_EXIT_POINT;
       
   140     }
       
   141 
       
   142 // Changes made for Context Framework API changes (SCB Id: MKAN-765D93)
       
   143 // ---------------------------------------------------------------------------
       
   144 // Returns an extension interface.
       
   145 // The extension interface is mapped with the extension UID.
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 TAny* CAlarmContextFwSupport::Extension( const TUid& /*aExtensionUid */ ) const
       
   149 {
       
   150 	TRACE_ENTRY_POINT;
       
   151   // Just return a NULL as currently there are no extensions available
       
   152   // and also since we do not support any of these APIs defined within
       
   153   // the alarm UID.
       
   154   TRACE_EXIT_POINT;
       
   155   return NULL;
       
   156 }
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // c++ constructor
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 CAlarmContextFwSupport::CAlarmContextFwSupport(CAlmAlarmControl* aAlarmControl)
       
   163     : iAlarmControl( aAlarmControl )
       
   164     {
       
   165     TRACE_ENTRY_POINT;
       
   166     TRACE_EXIT_POINT;
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // Symbian 2nd phase constructor.
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void CAlarmContextFwSupport::ConstructL()
       
   174     {
       
   175     TRACE_ENTRY_POINT;
       
   176     // Context Framework client side API
       
   177     OpenCFClientL();
       
   178 
       
   179     // define context(s)
       
   180     DefineContextL( KAlarmUISource, KAlarmUISourceState, KAlarmUISourceStateSec );
       
   181     DefineContextL( KAlarmUISource, KAlarmUISourceCommand, KAlarmUISourceCommandSec );
       
   182     DefineContextL( KAlarmUISource, KAlarmUISourceResult, KAlarmUISourceResultSec  );
       
   183 
       
   184     // define actions(s)
       
   185     DefineActionL( EActionSnooze );
       
   186 
       
   187     //Stop action definition should not be defined as the alarm component should not automatically stop the alarm on observing context events
       
   188     // but continue to expire the alarm and force the user to manually stop the alarm
       
   189     //DefineActionL( EActionStop );  
       
   190 
       
   191     // no need to keep the session open
       
   192     CloseCFClient();
       
   193     TRACE_EXIT_POINT;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // Defines a new context
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CAlarmContextFwSupport::DefineContextL( const TDesC& aContextSource,
       
   201                                              const TDesC& aContextType,
       
   202                                              const TSecurityPolicy& aSecurityPolicy )
       
   203     {
       
   204     TRACE_ENTRY_POINT;
       
   205     const TInt err = iCFClient->DefineContext( aContextSource,
       
   206                                                aContextType,
       
   207                                                aSecurityPolicy );
       
   208     // don't care if it already existed
       
   209     User::LeaveIfError( err == KErrAlreadyExists ? KErrNone : err );
       
   210     TRACE_EXIT_POINT;
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // Defines an action
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 void CAlarmContextFwSupport::DefineActionL(const TAlarmUIActionValues aAction)
       
   218 
       
   219     {
       
   220     TRACE_ENTRY_POINT;
       
   221     const TInt err = iCFClient->DefineAction( TPtrC( KAlarmUIActions[ aAction ] ),
       
   222                                               KAlarmUIActionSec );
       
   223     // don't care if it already existed
       
   224     User::LeaveIfError( err == KErrAlreadyExists ? KErrNone : err );
       
   225     TRACE_EXIT_POINT;
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // Subscribe an action
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 void CAlarmContextFwSupport::SubscribeActionL(const TAlarmUIActionValues aAction)
       
   233     {
       
   234     TRACE_ENTRY_POINT;
       
   235     CCFActionSubscription* as = CCFActionSubscription::NewLC();
       
   236     as->SetActionIdentifierL( TPtrC( KAlarmUIActions[ aAction ] ) );
       
   237     TInt err = iCFClient->SubscribeAction( *as );
       
   238 
       
   239     if( !err )
       
   240         {
       
   241         // add to array if the subscription was successfull
       
   242         err = iSubscribedActions.Append( as );
       
   243         CleanupStack::Pop( as );
       
   244         }
       
   245     else
       
   246         {
       
   247         // delete the object if it didn't go to the array
       
   248         CleanupStack::PopAndDestroy( as );
       
   249         }
       
   250     User::LeaveIfError( err );
       
   251     TRACE_EXIT_POINT;
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // Unsubscribe all actions
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 void CAlarmContextFwSupport::UnsubscribeActions()
       
   259     {
       
   260     TRACE_ENTRY_POINT;
       
   261     // unsubscribe all actions
       
   262     while( iSubscribedActions.Count() )
       
   263         {
       
   264         CCFActionSubscription* as = iSubscribedActions[0];
       
   265         iSubscribedActions.Remove( 0 );
       
   266         PIM_ASSERT( iCFClient->UnsubscribeAction( *as ); )
       
   267         delete as;
       
   268         }
       
   269     iSubscribedActions.Close();
       
   270     TRACE_EXIT_POINT;
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // Publish a context
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 void CAlarmContextFwSupport::PublishContextL( const TDesC& aSource,
       
   278                                               const TDesC& aType,
       
   279                                               const TDesC& aValue )
       
   280     {
       
   281     TRACE_ENTRY_POINT;
       
   282     CCFContextObject* co = CCFContextObject::NewLC( aSource, aType, aValue );
       
   283 
       
   284     const TInt err = iCFClient->PublishContext( *co );
       
   285     CleanupStack::PopAndDestroy( co );
       
   286     User::LeaveIfError( err );
       
   287     TRACE_EXIT_POINT;
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // Subscribe to actions/ context and publish the new alarm status.
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CAlarmContextFwSupport::StartL(const TAlarmSourceStateValues aEvent)
       
   295     {
       
   296     TRACE_ENTRY_POINT;
       
   297     // open client session(closed in StopL)
       
   298     OpenCFClientL();  // closed in StopL
       
   299 
       
   300     // subscribe for action(s)
       
   301     SubscribeActionL( EActionSnooze );
       
   302 
       
   303     //Subscription to stop action definition from the context events should not be defined as the alarm component should not automatically stop the alarm on observing context events
       
   304     // but continue to expire the alarm and force the user to manually stop the alarm
       
   305     //SubscribeActionL( EActionStop );
       
   306 
       
   307     // subscribe for context with Source:  'AlarmUI' & Type:    'Command'
       
   308     SubscribeContextL(KAlarmUISource, KAlarmUISourceCommand );
       
   309         
       
   310     // publish new alarm event/status
       
   311     PublishContextL( KAlarmUISource,
       
   312                      KAlarmUISourceState,
       
   313                      TPtrC( KAlarmUISourceStateValues[ aEvent ] ) );
       
   314     TRACE_EXIT_POINT;
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // publish 'alaram inactive' state and close the session
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 void CAlarmContextFwSupport::StopL()
       
   322     {
       
   323     TRACE_ENTRY_POINT;
       
   324     if( iCFClient ) // created in StartL
       
   325         {
       
   326         // publish the 'alarm inactive' status
       
   327         PublishContextL( KAlarmUISource,
       
   328                          KAlarmUISourceState,
       
   329                          TPtrC( KAlarmUISourceStateValues[ EStateAlarmInactive ] ) );
       
   330 
       
   331         // no need to keep the session open anymore
       
   332         CloseCFClient();
       
   333         }
       
   334     TRACE_EXIT_POINT;
       
   335     }
       
   336 
       
   337 // ---------------------------------------------------------------------------
       
   338 // Create a connection to Context Framework Server 
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void CAlarmContextFwSupport::OpenCFClientL()
       
   342     {
       
   343     TRACE_ENTRY_POINT;
       
   344     if( !iCFClient )
       
   345         {
       
   346         iCFClient = CCFClient::NewL( *this );
       
   347         }
       
   348     TRACE_EXIT_POINT;
       
   349     }
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // Unsusbscribe all actions and close the session.
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 void CAlarmContextFwSupport::CloseCFClient()
       
   356     {
       
   357     TRACE_ENTRY_POINT;
       
   358     UnsubscribeActions();
       
   359     
       
   360 	// unsubscribe contexts
       
   361     UnsubscribeContexts();
       
   362     delete iCFClient;
       
   363     iCFClient = NULL;
       
   364     TRACE_EXIT_POINT;
       
   365     }
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // publish context alarm result Source:  'AlarmUI' Type:    'Result'
       
   369 // ---------------------------------------------------------------------------
       
   370 //
       
   371 void CAlarmContextFwSupport::PublishAlarmResultL(const TAlarmSourceResultValues aResult)
       
   372     {
       
   373     TRACE_ENTRY_POINT;
       
   374     TBool closeCFClient( EFalse );
       
   375 
       
   376     // open a new CF client session if needed
       
   377     if( !iCFClient )
       
   378         {
       
   379         OpenCFClientL();
       
   380         closeCFClient = ETrue;
       
   381         }
       
   382 
       
   383     // publish the new alarm result
       
   384     PublishContextL( KAlarmUISource,
       
   385                      KAlarmUISourceResult,
       
   386                      TPtrC( KAlarmUISourceResultValues[ aResult ] ) );
       
   387 
       
   388     // close the CF client session if it was explicitly opened for this operation
       
   389     if( closeCFClient )
       
   390         {
       
   391         CloseCFClient();
       
   392         }
       
   393     TRACE_EXIT_POINT;
       
   394     }
       
   395 
       
   396 
       
   397 
       
   398 // ---------------------------------------------------------------------------
       
   399 // Subscribe a context
       
   400 // ---------------------------------------------------------------------------
       
   401 //
       
   402 void CAlarmContextFwSupport::SubscribeContextL( const TDesC& aSource, 
       
   403                           const TDesC& aType)
       
   404 
       
   405 	{
       
   406 	TRACE_ENTRY_POINT;
       
   407 	
       
   408 	// define a context
       
   409 	CCFContextSubscription* subscription = CCFContextSubscription::NewLC();
       
   410 	subscription->SetContextSourceL( aSource  );
       
   411     subscription->SetContextTypeL( aType );
       
   412     
       
   413     //subscribe to the context
       
   414     TInt err = iCFClient->SubscribeContext( *subscription );
       
   415 
       
   416     if( !err )
       
   417         {
       
   418         // add to array if the subscription was successfull
       
   419         err = iSubscribedContexts.Append( subscription );
       
   420         CleanupStack::Pop( subscription );
       
   421         }
       
   422     else
       
   423         {
       
   424         // delete the object if subscription fails
       
   425         CleanupStack::PopAndDestroy( subscription );
       
   426         }
       
   427     User::LeaveIfError( err );
       
   428         
       
   429     TRACE_EXIT_POINT;	
       
   430 	}
       
   431                           
       
   432 // ---------------------------------------------------------------------------
       
   433 // Unsubscribe all contexts
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CAlarmContextFwSupport :: UnsubscribeContexts()
       
   437 	
       
   438 	{
       
   439 	TRACE_ENTRY_POINT;
       
   440 	
       
   441     // unsubscribe all subscriptions
       
   442     while( iSubscribedContexts.Count() )
       
   443         {
       
   444         CCFContextSubscription* subscription = iSubscribedContexts[0];
       
   445         iSubscribedContexts.Remove( 0 );
       
   446         PIM_ASSERT( iCFClient->UnsubscribeContext( *subscription ) );
       
   447         delete subscription;
       
   448         }
       
   449     iSubscribedContexts.Close();
       
   450     
       
   451     TRACE_EXIT_POINT;	
       
   452 	}
       
   453 
       
   454 
       
   455 // End of File