calendarui/controller/src/calenstate.cpp
changeset 0 f979ecb2b13e
child 18 c198609911f9
child 19 1984aceb8774
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:   Calendar state machine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // includes
       
    21 #include "calendarui_debug.h"           // Debug macros
       
    22 #include "calenstate.h"                 // Calendar States
       
    23 #include "calencontroller.h"
       
    24 #include "calenstatemachine.h"
       
    25 #include "calennotifier.h"
       
    26 #include "calenasynccallback.h"
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // CCalenState::CCalenState
       
    30 // C++ constructor
       
    31 // ----------------------------------------------------------------------------
       
    32 CCalenState::CCalenState( CCalenController& aController,RHashSet<TCalenNotification>&  aOutstandingNotifications )
       
    33     : iController( aController ),
       
    34       iOutstandingNotifications( aOutstandingNotifications ),
       
    35       iPreviousState( CCalenStateMachine::ECalenIdleState )
       
    36     {
       
    37     TRACE_ENTRY_POINT;
       
    38 
       
    39     TRACE_EXIT_POINT;
       
    40     }
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // CCalenState::ConstructL
       
    44 // second phase construction
       
    45 // ----------------------------------------------------------------------------
       
    46 void CCalenState::BaseConstructL()
       
    47     {
       
    48     TRACE_ENTRY_POINT;
       
    49     
       
    50     TCalenCommand command;
       
    51     MCalenCommandHandler* handler( NULL );
       
    52     iCallBackPackage = new( ELeave ) CCalenCallbackPackage( this, command, handler );
       
    53     
       
    54     TRACE_EXIT_POINT;
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // CCalenState::~CCalenState
       
    59 // Destructor
       
    60 // ----------------------------------------------------------------------------
       
    61 CCalenState::~CCalenState()
       
    62     {
       
    63     TRACE_ENTRY_POINT;
       
    64     
       
    65     delete iCallBackPackage;
       
    66     iCallBackPackage = NULL;
       
    67     delete iCmdCallback;
       
    68     
       
    69     TRACE_EXIT_POINT;
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // CCalenState::RequestCallback
       
    74 // Request a callback
       
    75 // ----------------------------------------------------------------------------
       
    76 void CCalenState::RequestCallbackL( MCalenCommandHandler* aCommandHandler, const TCalenCommand& aCommand )
       
    77     {
       
    78     TRACE_ENTRY_POINT;
       
    79 
       
    80     iCallBackPackage->SetCommandHandler(this, aCommand, aCommandHandler);
       
    81      
       
    82     if( !iCmdCallback )
       
    83         {
       
    84         TCallBack callback( CommandCallback, iCallBackPackage );
       
    85         iCmdCallback = CCalenAsyncCallBack::NewL( callback, CActive::EPriorityHigh , iController );
       
    86        }
       
    87 
       
    88     TCallBack callback( CommandCallback, iCallBackPackage );
       
    89     
       
    90     iCmdCallback->Cancel();
       
    91     iCmdCallback->Set( callback );
       
    92     iCmdCallback->CallBack();
       
    93     
       
    94     TRACE_EXIT_POINT;
       
    95     }
       
    96 
       
    97 
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // CCalenState::CommandCallback
       
   101 // Static callback function
       
   102 // ----------------------------------------------------------------------------
       
   103 TInt CCalenState::CommandCallback( TAny* aCommandStruct )
       
   104     {
       
   105     TRACE_ENTRY_POINT;
       
   106     TBool continueCommand(EFalse);
       
   107     
       
   108     CCalenCallbackPackage* package = static_cast<CCalenCallbackPackage*>( aCommandStruct );
       
   109     continueCommand = package->HandleCallBack();
       
   110     
       
   111     TRACE_EXIT_POINT;
       
   112     return continueCommand;
       
   113     }
       
   114 
       
   115 // ----------------------------------------------------------------------------
       
   116 // CCalenState::HandleNotificationL
       
   117 // Default behaviour for notification handling is to go back to previous state.
       
   118 // Concrete states would be expected to check the notification is related
       
   119 // to the command issued.
       
   120 // ----------------------------------------------------------------------------
       
   121 void CCalenState::HandleNotificationL(const TCalenNotification& aNotification,
       
   122                                       CCalenStateMachine& aStateMachine )
       
   123     {
       
   124     TRACE_ENTRY_POINT;
       
   125     
       
   126     switch( aNotification )
       
   127         {
       
   128         case ECalenNotifyAppBackgrounded:
       
   129         	{
       
   130         	CCalenStateMachine::TCalenStateIndex cachedState = aStateMachine.GetCurrentState();
       
   131         	if(cachedState != CCalenStateMachine::ECalenMapState) // Never send calendar to background state in MapState as maps will 
       
   132         	                                                        // launched in cahin mode not in stand alone mode
       
   133         	    {
       
   134                 aStateMachine.SetCurrentState(CCalenStateMachine::ECalenBackgroundState);
       
   135                 aStateMachine.SetCurrentPreviousState(cachedState);
       
   136                 iOutstandingNotifications.InsertL(aNotification);
       
   137                 aStateMachine.ActivateCurrentStateL();
       
   138         	    }
       
   139         	}
       
   140             break;
       
   141         default:
       
   142             iController.Notifier().BroadcastApprovedNotification( aNotification );
       
   143             break;
       
   144         }
       
   145     
       
   146    
       
   147     TRACE_EXIT_POINT;
       
   148 
       
   149     }
       
   150 
       
   151 
       
   152 // ----------------------------------------------------------------------------
       
   153 // CCalenState::PreviousState
       
   154 // C++ constructor
       
   155 // ----------------------------------------------------------------------------
       
   156 CCalenStateMachine::TCalenStateIndex CCalenState::PreviousState()
       
   157     {
       
   158     TRACE_ENTRY_POINT;
       
   159     
       
   160     TRACE_EXIT_POINT;
       
   161     return iPreviousState;
       
   162     }
       
   163 
       
   164 // ----------------------------------------------------------------------------
       
   165 // CCalenState::SetPreviousState
       
   166 // Sets previous state.
       
   167 // ----------------------------------------------------------------------------
       
   168 void CCalenState::SetPreviousState(const CCalenStateMachine::TCalenStateIndex& aPreviousState )
       
   169     {
       
   170     TRACE_ENTRY_POINT;
       
   171     
       
   172     iPreviousState = aPreviousState;
       
   173     
       
   174     TRACE_EXIT_POINT;
       
   175     }
       
   176     
       
   177 // ----------------------------------------------------------------------------
       
   178 // CCalenState::CommandExecuting
       
   179 // From CCalenState
       
   180 // ----------------------------------------------------------------------------    
       
   181 void CCalenState::CommandExecuting()
       
   182     {
       
   183     TRACE_ENTRY_POINT;
       
   184     TRACE_EXIT_POINT;
       
   185     }  
       
   186 
       
   187 // ----------------------------------------------------------------------------
       
   188 // CCalenState::CCalenCallbackPackage::CCalenCallbackPackage
       
   189 // C++ constructor
       
   190 // ----------------------------------------------------------------------------
       
   191 CCalenState::CCalenCallbackPackage::CCalenCallbackPackage( CCalenState* aSelf,
       
   192                         TCalenCommand aCommand,  MCalenCommandHandler* aCommandHandler)
       
   193     : iSelf( aSelf ), iCommand( aCommand ), iCommandHandler( aCommandHandler )                   
       
   194     {
       
   195     TRACE_ENTRY_POINT;
       
   196     TRACE_EXIT_POINT;
       
   197     }
       
   198 
       
   199 // ----------------------------------------------------------------------------
       
   200 // CCalenState::CCalenCallbackPackage::HandleCallBack
       
   201 // Handles a callback
       
   202 // ----------------------------------------------------------------------------
       
   203 TBool CCalenState::CCalenCallbackPackage::HandleCallBack()
       
   204     {
       
   205     TRACE_ENTRY_POINT;
       
   206     TBool continueCommand(EFalse);
       
   207 
       
   208     iSelf->CommandExecuting();
       
   209     PIM_TRAPD_HANDLE ( continueCommand = iCommandHandler->HandleCommandL( iCommand ) );
       
   210     
       
   211     TRACE_EXIT_POINT;
       
   212     return continueCommand;
       
   213     }
       
   214 
       
   215 // ----------------------------------------------------------------------------
       
   216 // CCalenState::CCalenCallbackPackage::SetCommandHandler
       
   217 // Sets the command handler and command
       
   218 // ----------------------------------------------------------------------------
       
   219 void CCalenState::CCalenCallbackPackage::SetCommandHandler(CCalenState* aSelf,
       
   220                         TCalenCommand aCommand,  MCalenCommandHandler* aCommandHandler)
       
   221     {
       
   222     TRACE_ENTRY_POINT;
       
   223         
       
   224     iSelf = aSelf;
       
   225     iCommand = aCommand;
       
   226     iCommandHandler = aCommandHandler; 
       
   227 
       
   228     TRACE_EXIT_POINT;    
       
   229     }
       
   230     
       
   231 // ----------------------------------------------------------------------------
       
   232 // CCalenState::HandleStateActivationL
       
   233 // Default behavior when state is activated. Check and issue any outstanding
       
   234 // notifications
       
   235 // ----------------------------------------------------------------------------
       
   236 void CCalenState::HandleStateActivationL(CCalenStateMachine& /*aStateMachine*/)
       
   237 	{
       
   238     TRACE_ENTRY_POINT;
       
   239     
       
   240     // Can only issue one of the following Notifications
       
   241     // ECalenNotifySettingsChanged or 
       
   242     // ECalenNotifySystemLocaleChanged or
       
   243     // ECalenNotifySettingsClosed
       
   244     
       
   245     if(iOutstandingNotifications.Find(ECalenNotifySettingsChanged))
       
   246         {
       
   247         iOutstandingNotifications.Remove(ECalenNotifySystemLocaleChanged);
       
   248         iOutstandingNotifications.Remove(ECalenNotifySettingsClosed);
       
   249         }
       
   250     else if(iOutstandingNotifications.Find(ECalenNotifySystemLocaleChanged))
       
   251         {
       
   252         iOutstandingNotifications.Remove(ECalenNotifySettingsClosed);
       
   253         }
       
   254     
       
   255     THashSetIter<TCalenNotification> iterator(iOutstandingNotifications);
       
   256     const TCalenNotification* notification = iterator.Next();
       
   257     while(notification)
       
   258         {
       
   259         iController.Notifier().BroadcastApprovedNotification( *notification );
       
   260         notification = iterator.Next();
       
   261         }
       
   262     
       
   263     iOutstandingNotifications.Close();
       
   264     
       
   265     TRACE_EXIT_POINT;
       
   266 	}
       
   267 
       
   268 // ----------------------------------------------------------------------------
       
   269 // CCalenState::SetCurrentState
       
   270 // Sets the current state
       
   271 // ----------------------------------------------------------------------------
       
   272 void CCalenState::SetCurrentState(CCalenStateMachine& aStateMachine,
       
   273 								  const CCalenStateMachine::TCalenStateIndex& aState )	
       
   274 	{
       
   275     TRACE_ENTRY_POINT;
       
   276     
       
   277     aStateMachine.SetCurrentState(aState);
       
   278     
       
   279     TRACE_EXIT_POINT;	
       
   280 	}
       
   281 
       
   282 // ----------------------------------------------------------------------------
       
   283 // CCalenState::GetCurrentState
       
   284 // Gets the current state
       
   285 // ----------------------------------------------------------------------------
       
   286 CCalenStateMachine::TCalenStateIndex CCalenState::GetCurrentState(
       
   287 											CCalenStateMachine& aStateMachine)
       
   288 	{
       
   289 	TRACE_ENTRY_POINT;
       
   290 	TRACE_EXIT_POINT;
       
   291 	
       
   292 	return aStateMachine.GetCurrentState();
       
   293 	}
       
   294 
       
   295 // ----------------------------------------------------------------------------
       
   296 // CCalenState::SetPreviousState
       
   297 // Sets the previous state through state machine
       
   298 // ----------------------------------------------------------------------------
       
   299 void CCalenState::SetCurrentPreviousState(CCalenStateMachine& aStateMachine, 
       
   300 								const CCalenStateMachine::TCalenStateIndex& aState)
       
   301 	{
       
   302 	TRACE_ENTRY_POINT;
       
   303 	
       
   304 	aStateMachine.SetCurrentPreviousState(aState);
       
   305 	
       
   306 	TRACE_EXIT_POINT;
       
   307 	}
       
   308 
       
   309 // ----------------------------------------------------------------------------
       
   310 // CCalenState::ActivateCurrentStateL
       
   311 // Activate current state
       
   312 // ----------------------------------------------------------------------------
       
   313 void CCalenState::ActivateCurrentStateL(CCalenStateMachine& aStateMachine)
       
   314 	{
       
   315     TRACE_ENTRY_POINT;
       
   316 	
       
   317     aStateMachine.ActivateCurrentStateL();
       
   318     	
       
   319     TRACE_EXIT_POINT;	
       
   320 	}
       
   321 // ----------------------------------------------------------------------------
       
   322 // CCalenState::CancelPreviousCmd
       
   323 // Cancel the previous command
       
   324 // ----------------------------------------------------------------------------
       
   325 void CCalenState::CancelPreviousCmd(CCalenStateMachine& aStateMachine)
       
   326 	{
       
   327 	TRACE_ENTRY_POINT;
       
   328 	
       
   329 	aStateMachine.CancelPreviousCmd();
       
   330 	
       
   331 	TRACE_EXIT_POINT;
       
   332 	}
       
   333 
       
   334 // ----------------------------------------------------------------------------
       
   335 // CCalenState::CancelExecutingCmd
       
   336 // Cancels executing command
       
   337 // ----------------------------------------------------------------------------
       
   338 void CCalenState::CancelExecutingCmd()
       
   339 	{
       
   340     TRACE_ENTRY_POINT;
       
   341 	
       
   342 	if(iCmdCallback)
       
   343 		{
       
   344 		iCmdCallback->Cancel();
       
   345 		}
       
   346     	
       
   347     TRACE_EXIT_POINT;	
       
   348 	}	
       
   349 // End of file