alarmui/src/alarmutils.cpp
changeset 0 f979ecb2b13e
child 18 c198609911f9
child 20 21239b3bcd78
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2007-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:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "alarmutils.h"
       
    22 
       
    23 #include "AlmAlertVariant.hrh"
       
    24 #include "pim_trace.h"
       
    25 
       
    26 #include "AlmAlarmPlayer.h"
       
    27 #include "AlmAlarmControl.h"
       
    28 #include "AlmAlarmInfo.h"
       
    29 #include "AlarmService.h"
       
    30 #include "alarmremconeventshandler.h"
       
    31 #ifdef RD_ALMALERT__SENSOR_SUPPORT
       
    32 #include "alarmcontextfwsupport.h"
       
    33 #endif // RD_ALMALERT__SENSOR_SUPPORT
       
    34 
       
    35 #include <almconst.h>
       
    36 #include <eikenv.h>
       
    37 #include <AknCapServer.h>
       
    38 #include <e32property.h>
       
    39 #include <centralrepository.h>
       
    40 #include <MProfileEngine.h>
       
    41 #include <MProfile.h>
       
    42 #include <MProfileTones.h>
       
    43 #include <ProfileEngineDomainConstants.h>
       
    44 #include <sbdefs.h>
       
    45 #include <coreapplicationuisdomainpskeys.h>
       
    46 #include <clockdomaincrkeys.h>
       
    47 #include <CalendarInternalCRKeys.h>
       
    48 #include <wakeupalarm.h>
       
    49 #include <calalarm.h> // KUidAgendaModelAlarmCategory - the alarm category id for calendar alarms
       
    50 #include <AknUtils.h>
       
    51 
       
    52 #ifndef SYMBIAN_CALENDAR_V2
       
    53 #include <agmalarm.h> // deprecated, use CalAlarm.h when SYMBIAN_CALENDAR_V2 flag is enabled
       
    54 #endif // SYMBIAN_CALENDAR_V2
       
    55 #include <calsession.h>
       
    56 #include <calentryview.h>
       
    57 #include <calenlauncher.h>  // for launching calendar entry view
       
    58 
       
    59 
       
    60 
       
    61 // CONSTANTS AND MACROS
       
    62 const TInt KAlmAlertMinSnooze( 100 );
       
    63 const TInt KAlmAlertMaxSnooze( 104 );
       
    64 
       
    65 const TInt KDefaultSnoozeTime( 5 ); // 5 minutes
       
    66 
       
    67 const TUint KAlarmAutoHide( 60000000 );  // 60 s
       
    68 const TUint KAlarmAutoHideCalendar( 30000000 );  // 30 s
       
    69 const TUint KKeyBlockTime( 500000 );  // 0.5 s
       
    70 const TUint KShutdownTime( 1500000 ); // 1.5 s
       
    71 const TUint KAlarmDelayTime( 1000000 ); // 1.0 s
       
    72 const TUint KInactivityResetInterval( 1000000 ); // 1.0 s
       
    73 
       
    74 const TInt KMaxProfileVolume( 10 );
       
    75 const TInt KVolumeRampPeriod( 3000000 );  // 3 seconds
       
    76 _LIT( KRngMimeType, "application/vnd.nokia.ringing-tone" );
       
    77 
       
    78 
       
    79 // ==========================================================
       
    80 // ================= MEMBER FUNCTIONS =======================
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------
       
    84 // 
       
    85 // ---------------------------------------------------------
       
    86 //
       
    87 CAlarmUtils* CAlarmUtils::NewL( CAlmAlarmControl* aAlarmControl,
       
    88                                 CAknAlarmService* aSupervisor )
       
    89     {
       
    90     TRACE_ENTRY_POINT;
       
    91     CAlarmUtils* self = new( ELeave )CAlarmUtils( aAlarmControl, aSupervisor );
       
    92     CleanupStack::PushL( self );
       
    93     self->ConstructL();
       
    94     CleanupStack::Pop( self );
       
    95     TRACE_EXIT_POINT;
       
    96     return self;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // 
       
   101 // ---------------------------------------------------------
       
   102 //
       
   103 void CAlarmUtils::ConstructL()
       
   104     {
       
   105     TRACE_ENTRY_POINT;
       
   106     User::LeaveIfError( iApaSession.Connect() );
       
   107 
       
   108     iAlarmInfo = new( ELeave )CAlmAlarmInfo( this );
       
   109 
       
   110     // we can still work without profile engine
       
   111     PIM_TRAPD_ASSERT( iProfileEng = CreateProfileEngineL(); )
       
   112 
       
   113     iNotifierDialogController = ((CAknCapServer*)CEikonEnv::Static()->AppServer())->GlobalNoteControllerL();
       
   114     iNotifierDialogController->SetNoteObserver( iAlarmControl );
       
   115 
       
   116     // create timers
       
   117     iShutdownTimer   = CPeriodic::NewL( CActive::EPriorityStandard );
       
   118     iAutoSnoozeTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   119     iKeyBlockTimer   = CPeriodic::NewL( CActive::EPriorityStandard );
       
   120     iAlarmDelayTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   121     iInactivityResetTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   122     // set async callback priority so that it finishes before any of the timers
       
   123     iAsyncCallBack = new( ELeave )CAsyncCallBack( CActive::EPriorityStandard +1 );
       
   124 
       
   125     //RD_ALMALERT__ACCESSORY_STOP removed as a fix for ABAA-74R757
       
   126     // accessory observer
       
   127     iRemConHandler = CAlarmRemConEventsHandler::NewL( *iAlarmControl );
       
   128     
       
   129     iCalendarAlarmViewer = EFalse;
       
   130 
       
   131     TRACE_EXIT_POINT;
       
   132     }
       
   133 
       
   134 // ---------------------------------------------------------
       
   135 // C++ default constructor
       
   136 // ---------------------------------------------------------
       
   137 //
       
   138 CAlarmUtils::CAlarmUtils( CAlmAlarmControl* aAlarmControl, 
       
   139                           CAknAlarmService* aSupervisor )
       
   140     : iAlarmControl( aAlarmControl ),
       
   141       iSupervisor( aSupervisor ),
       
   142       iLightsBlinking( EFalse ),
       
   143       iKeyGuardActivated( ETrue )
       
   144     {
       
   145     TRACE_ENTRY_POINT;
       
   146     TRACE_EXIT_POINT;
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------
       
   150 // Destructor
       
   151 // ---------------------------------------------------------
       
   152 //
       
   153 CAlarmUtils::~CAlarmUtils()
       
   154     {
       
   155     TRACE_ENTRY_POINT;
       
   156     if( iNotifierDialogController )
       
   157         {
       
   158         iNotifierDialogController->SetNoteObserver( NULL );
       
   159         }
       
   160 
       
   161     if( iShutdownTimer )
       
   162         {
       
   163         iShutdownTimer->Cancel();
       
   164         delete iShutdownTimer;
       
   165         iShutdownTimer = NULL;
       
   166         }
       
   167 
       
   168     if( iAutoSnoozeTimer )
       
   169         {
       
   170         iAutoSnoozeTimer->Cancel();
       
   171         delete iAutoSnoozeTimer;
       
   172         iAutoSnoozeTimer = NULL;
       
   173         }
       
   174 
       
   175     if( iKeyBlockTimer )
       
   176         {
       
   177         iKeyBlockTimer->Cancel();
       
   178         delete iKeyBlockTimer;
       
   179         iKeyBlockTimer = NULL;
       
   180         }
       
   181 
       
   182     if( iAlarmDelayTimer )
       
   183         {
       
   184         iAlarmDelayTimer->Cancel();
       
   185         delete iAlarmDelayTimer;
       
   186         iAlarmDelayTimer = NULL;
       
   187         }
       
   188 
       
   189     if( iInactivityResetTimer )
       
   190         {
       
   191         iInactivityResetTimer->Cancel();
       
   192         delete iInactivityResetTimer;
       
   193         iInactivityResetTimer = NULL;
       
   194         }
       
   195 
       
   196     if( iAsyncCallBack )
       
   197         {
       
   198         iAsyncCallBack->Cancel();
       
   199         delete iAsyncCallBack;
       
   200         iAsyncCallBack = NULL;
       
   201         }
       
   202 
       
   203     delete iAlarmPlayer;
       
   204     iAlarmPlayer = NULL;
       
   205 
       
   206     delete iAlarmInfo;
       
   207     iAlarmInfo = NULL;
       
   208 
       
   209     delete iRemConHandler;
       
   210     iRemConHandler = NULL;
       
   211 
       
   212     #ifdef RD_ALMALERT__SENSOR_SUPPORT
       
   213     delete iCFSupport;
       
   214     iCFSupport = NULL;
       
   215     #endif // RD_ALMALERT__SENSOR_SUPPORT
       
   216 
       
   217     if( iProfileEng )
       
   218         {
       
   219         iProfileEng->Release();
       
   220         iProfileEng = NULL;
       
   221         }
       
   222 
       
   223     iApaSession.Close();
       
   224 	
       
   225 	if(iQueueManagerAsw)
       
   226 		{
       
   227 		delete iQueueManagerAsw;
       
   228 		}
       
   229 	if(iEntryAsyncWait)
       
   230 		{
       
   231 		delete iEntryAsyncWait;
       
   232 		}
       
   233 		
       
   234 	if(iCalSession)
       
   235 		{
       
   236 		delete iCalSession;
       
   237 		}
       
   238 
       
   239 	if(iCalEntryView)
       
   240 		{
       
   241 		delete iCalEntryView;
       
   242 		}
       
   243 
       
   244     TRACE_EXIT_POINT;
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------
       
   248 // Start the alarm player.
       
   249 // ---------------------------------------------------------
       
   250 //
       
   251 void CAlarmUtils::PlayAlarmSound()
       
   252     {
       
   253     TRACE_ENTRY_POINT;
       
   254     StopAlarmSound();
       
   255     PIM_TRAPD_ASSERT( iAlarmPlayer = CAlmAlarmPlayer::NewL( this ); )
       
   256     SetBackLight( ETrue );
       
   257     TRACE_EXIT_POINT;
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------
       
   261 // Stop the alarm player.
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 void CAlarmUtils::StopAlarmSound()
       
   265     {
       
   266     TRACE_ENTRY_POINT;
       
   267     #if defined( RD_ALMALERT__SENSOR_SUPPORT )
       
   268     // notify the result through the context framework
       
   269     // only set the context if the player was really active
       
   270     if( iAlarmPlayer && iCFSupport )
       
   271         {
       
   272         PIM_TRAPD_ASSERT( iCFSupport->PublishAlarmResultL( EResultAlarmSilenced ); )
       
   273         }
       
   274     #endif // RD_ALMALERT__SENSOR_SUPPORT
       
   275     delete iAlarmPlayer;
       
   276     iAlarmPlayer = NULL;
       
   277     SetBackLight( EFalse );
       
   278     TRACE_EXIT_POINT;
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------
       
   282 // Backlight on/off
       
   283 // ---------------------------------------------------------
       
   284 //
       
   285 void CAlarmUtils::SetBackLight(TBool aActive)
       
   286     {
       
   287     TRACE_ENTRY_POINT;
       
   288 	TInt status;
       
   289 	// reading the value before setting. This also ensures if the P&S key is defined or not. 
       
   290 	TInt ret = RProperty::Get( KPSUidCoreApplicationUIs, 
       
   291 									KLightsAlarmLightActive, 
       
   292 									status); 
       
   293 
       
   294 	if(ret == KErrNotFound )
       
   295 		{	
       
   296 		return ;
       
   297 		}
       
   298 
       
   299     if( iLightsBlinking != aActive )
       
   300         {
       
   301         iLightsBlinking = aActive;
       
   302         PIM_ASSERT( RProperty::Set( KPSUidCoreApplicationUIs, 
       
   303                                     KLightsAlarmLightActive, 
       
   304                                     aActive ? ELightsBlinking : ELightsNotBlinking ); )
       
   305         }
       
   306     TRACE_EXIT_POINT;
       
   307     }
       
   308 
       
   309 // ---------------------------------------------------------
       
   310 // Key guard on/off
       
   311 // ---------------------------------------------------------
       
   312 //
       
   313 void CAlarmUtils::SetKeyGuard(TBool aActive)
       
   314     {
       
   315     TRACE_ENTRY_POINT;
       
   316     if( iKeyGuardActivated != aActive )
       
   317         {
       
   318         iKeyGuardActivated = aActive;
       
   319         PIM_ASSERT( RProperty::Set( KPSUidCoreApplicationUIs, 
       
   320                                     KCoreAppUIsDisableKeyguard, 
       
   321                                     aActive ? ECoreAppUIsEnableKeyguard : ECoreAppUIsDisableKeyguard ); )
       
   322         }
       
   323     TRACE_EXIT_POINT;
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------
       
   327 // 
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 TBool CAlarmUtils::IsClockAlarm() const
       
   331     {
       
   332     TRACE_ENTRY_POINT;
       
   333     const TBool retVal( iAlarmData.iAlarm.Category() == KAlarmClockOne );
       
   334     TRACE_EXIT_POINT;
       
   335     return retVal;
       
   336     }
       
   337 
       
   338 // ---------------------------------------------------------
       
   339 // 
       
   340 // ---------------------------------------------------------
       
   341 //
       
   342 TBool CAlarmUtils::IsCalendarAlarm() const
       
   343     {
       
   344     TRACE_ENTRY_POINT;
       
   345     const TBool retVal( iAlarmData.iAlarm.Category() == KUidAgendaModelAlarmCategory );
       
   346     TRACE_EXIT_POINT;
       
   347     return retVal;
       
   348     }
       
   349 
       
   350 // ---------------------------------------------------------
       
   351 // Check the active profile ringing type.
       
   352 // ---------------------------------------------------------
       
   353 //
       
   354 TBool CAlarmUtils::IsRingingTypeSilent() const
       
   355     {
       
   356     TRACE_ENTRY_POINT;
       
   357     TBool isSilent( EFalse );
       
   358 
       
   359     if( iProfileEng )
       
   360         {
       
   361         MProfile* profile = NULL;
       
   362         PIM_TRAPD_ASSERT( profile = iProfileEng->ActiveProfileL(); )
       
   363 
       
   364         if( profile )
       
   365             {
       
   366             if( profile->ProfileTones().ToneSettings().iRingingType == EProfileRingingTypeSilent ||
       
   367                 profile->ProfileTones().ToneSettings().iRingingType == EProfileRingingTypeBeepOnce )
       
   368                 {
       
   369                 isSilent = ETrue;  // Ringing Type = Silent or BeepOnce
       
   370                 }
       
   371             profile->Release();
       
   372             }
       
   373         }
       
   374     TRACE_EXIT_POINT;
       
   375     return isSilent;
       
   376     }
       
   377 
       
   378 // ---------------------------------------------------------
       
   379 // Check if the used alarm tone is an "off" tone.
       
   380 // ---------------------------------------------------------
       
   381 //
       
   382 TBool CAlarmUtils::IsOffTone() const
       
   383     {
       
   384     TRACE_ENTRY_POINT;
       
   385     TBool isOffTone( EFalse );
       
   386 
       
   387     if( iAlarmData.iAlarmTone == KNullDesC )
       
   388         {
       
   389         isOffTone = ETrue;
       
   390         }
       
   391     else
       
   392         {
       
   393         // alarm tone file path w/o drive name
       
   394         TPtrC alarmToneFilePath = 
       
   395             iAlarmData.iAlarmTone.Mid( 
       
   396                 TParsePtrC( iAlarmData.iAlarmTone ).Drive().Length() );
       
   397 
       
   398         if( alarmToneFilePath == KProfileNoSoundPath )
       
   399             {
       
   400             isOffTone = ETrue;
       
   401             }
       
   402         }
       
   403     TRACE_EXIT_POINT;
       
   404     return isOffTone;
       
   405     }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // Read alarm sound filenames from central repository.
       
   409 // -----------------------------------------------------------------------------
       
   410 //
       
   411 void CAlarmUtils::GetAlarmSoundFilenames()
       
   412     {
       
   413     TRACE_ENTRY_POINT;
       
   414     CRepository* repository = NULL;
       
   415 
       
   416     if( IsClockAlarm() )
       
   417         {
       
   418         PIM_TRAPD_ASSERT( repository = CRepository::NewL( KCRUidClockApp ); )
       
   419 
       
   420         if( repository )
       
   421             {
       
   422             PIM_ASSERT( repository->Get( KClockAppSoundFile, iAlarmData.iAlarmTone ); )
       
   423             PIM_ASSERT( repository->Get( KClockAppDefaultSoundFile, iAlarmData.iDefaultAlarmTone ); )
       
   424             }
       
   425         }
       
   426     else
       
   427         {
       
   428         PIM_TRAPD_ASSERT( repository = CRepository::NewL( KCRUidCalendar ); )
       
   429 
       
   430         if( repository )
       
   431             {
       
   432             PIM_ASSERT( repository->Get( KCalendarSoundFile, iAlarmData.iAlarmTone ); )
       
   433             PIM_ASSERT( repository->Get( KCalendarDefaultSoundFile, iAlarmData.iDefaultAlarmTone ); )
       
   434             }
       
   435         }
       
   436     delete repository;
       
   437     TRACE_EXIT_POINT;
       
   438     }
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // Check if the alarm sound file is a KRngMimeType file.
       
   442 // -----------------------------------------------------------------------------
       
   443 //
       
   444 TBool CAlarmUtils::IsRngTone()
       
   445     {
       
   446     TRACE_ENTRY_POINT;
       
   447     TBool isRngTone( EFalse );
       
   448 
       
   449     RFs& session = iAlarmControl->ControlEnv()->FsSession();
       
   450     // Check that the file exist.  If not, use the default file.
       
   451     TUint attributes;
       
   452     TInt error = session.Att( iAlarmData.iAlarmTone, attributes );
       
   453 
       
   454     if( error )
       
   455         {
       
   456         iAlarmData.iAlarmTone = iAlarmData.iDefaultAlarmTone;
       
   457         }
       
   458 
       
   459     TUid dummyUid( KNullUid );
       
   460     TDataType dataType;
       
   461 
       
   462     error = iApaSession.AppForDocument( iAlarmData.iAlarmTone, dummyUid, dataType );
       
   463     ASSERT( !error );
       
   464 
       
   465     if( !error && dataType.Des().CompareF( KRngMimeType ) == 0 )
       
   466         {
       
   467         isRngTone = ETrue;
       
   468         }
       
   469     TRACE_EXIT_POINT;
       
   470     return isRngTone;
       
   471     }
       
   472 
       
   473 // -----------------------------------------------------------------------------
       
   474 // Read active profile settings from the profile engine.
       
   475 // -----------------------------------------------------------------------------
       
   476 //
       
   477 void CAlarmUtils::GetProfileSettings()
       
   478     {
       
   479     TRACE_ENTRY_POINT;
       
   480     // set default/fallback values
       
   481     iAlarmData.iRingType = EProfileRingingTypeRinging; // not used with clock alarm
       
   482     iAlarmData.iRepeatValue = KMdaAudioToneRepeatForever; // KMdaRepeatForever
       
   483     iAlarmData.iVolume = KMaxProfileVolume; // clock alarms always ramp to maximum volume
       
   484     iAlarmData.iVolumeRampTime = iAlarmData.iVolume * KVolumeRampPeriod; // volume ramp only for clock alarms
       
   485 
       
   486     if( !IsClockAlarm() )
       
   487         {
       
   488         if( iProfileEng )
       
   489             {
       
   490             MProfile* profile = NULL;
       
   491             PIM_TRAPD_ASSERT( profile = iProfileEng->ActiveProfileL(); )
       
   492 
       
   493             if( profile )
       
   494                 {
       
   495                 const TProfileToneSettings& setting = profile->ProfileTones().ToneSettings();
       
   496                 iAlarmData.iRingType = setting.iRingingType;
       
   497 
       
   498                 // calendar alarms: if profile is "silent" -> set volume to zero
       
   499                 iAlarmData.iVolume = (iAlarmData.iRingType == EProfileRingingTypeSilent ? 0 : setting.iRingingVolume);
       
   500 
       
   501                 profile->Release();
       
   502                 }
       
   503             }
       
   504 
       
   505         if( iAlarmData.iRingType == EProfileRingingTypeRingingOnce )
       
   506             {
       
   507             iAlarmData.iRepeatValue = 1;
       
   508             }
       
   509         iAlarmData.iVolumeRampTime = 0;
       
   510         }
       
   511     TRACE_EXIT_POINT;
       
   512     }
       
   513 
       
   514 // -----------------------------------------------------------------------------
       
   515 // 
       
   516 // -----------------------------------------------------------------------------
       
   517 //
       
   518 TInt CAlarmUtils::MaxProfileVolume() const
       
   519     {
       
   520     TRACE_ENTRY_POINT;
       
   521     TRACE_EXIT_POINT;
       
   522     return KMaxProfileVolume;
       
   523     }
       
   524 
       
   525 // -----------------------------------------------------------------------------
       
   526 // Access to iAlarmData structure.
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 CAlarmUtils::TAlarmData& CAlarmUtils::AlarmData()
       
   530     {
       
   531     TRACE_ENTRY_POINT;
       
   532     TRACE_EXIT_POINT;
       
   533     return iAlarmData;
       
   534     }
       
   535 
       
   536 // -----------------------------------------------------------------------------
       
   537 // Update the iAlarmData structure.
       
   538 // NOTE! Only to be called from CAlmAlarmControl::ShowAlarm.
       
   539 // -----------------------------------------------------------------------------
       
   540 //
       
   541 void CAlarmUtils::InitialiseAlarmDataL()
       
   542     {
       
   543     TRACE_ENTRY_POINT;
       
   544     // set alarm type first!
       
   545     iAlarmData.iAlarmType = IsClockAlarm() ? EAlarmTypeClock : 
       
   546                                 IsCalendarAlarm() ? EAlarmTypeCalendar : 
       
   547                                     EAlarmTypeOther;
       
   548     GetSnoozeTimeSetting();
       
   549     GetProfileSettings();
       
   550     GetAlarmSoundFilenames();
       
   551 
       
   552     // get calendar entry data and store the local uid and instance time
       
   553     GetCalendarEntryInfoL();
       
   554     TRACE_EXIT_POINT;
       
   555     }
       
   556 
       
   557 // -----------------------------------------------------------------------------
       
   558 // Update the iAlarmData structure.
       
   559 // NOTE! Only to be called from CAlmAlarmControl::ShowAlarm.
       
   560 // -----------------------------------------------------------------------------
       
   561 //
       
   562 void CAlarmUtils::UninitialiseAlarmData()
       
   563     {
       
   564     TRACE_ENTRY_POINT;
       
   565 
       
   566 	SetCalendarViewStatus(ECalViewCreateNone);
       
   567 
       
   568 	if(iEntryAsyncWait)
       
   569 		{
       
   570 		delete iEntryAsyncWait;
       
   571 		iEntryAsyncWait =  NULL;
       
   572 		}
       
   573 	
       
   574 	if(iQueueManagerAsw)
       
   575 		{
       
   576 		delete iQueueManagerAsw;
       
   577 		iQueueManagerAsw =  NULL;
       
   578 		}
       
   579 
       
   580 	if(iCalEntryView)
       
   581 		{
       
   582 		delete iCalEntryView;
       
   583 		iCalEntryView= NULL;
       
   584 		}
       
   585 	
       
   586 	if(iCalSession)
       
   587 		{
       
   588 		delete iCalSession;
       
   589 		iCalSession =  NULL;
       
   590 		}
       
   591 
       
   592     TRACE_EXIT_POINT;
       
   593     }
       
   594 
       
   595 // ---------------------------------------------------------
       
   596 // Check the snooze count
       
   597 // ---------------------------------------------------------
       
   598 //
       
   599 TBool CAlarmUtils::CanSnooze() const
       
   600     {
       
   601     TRACE_ENTRY_POINT;
       
   602     const TInt snoozeCount( iAlarmData.iAlarm.ClientData1() );
       
   603     // can't snooze if snoozeCount equals KAlmAlertMinSnooze
       
   604     const TBool retVal = !(snoozeCount == KAlmAlertMinSnooze);
       
   605     TRACE_EXIT_POINT;
       
   606     return retVal;
       
   607     }
       
   608 
       
   609 // ---------------------------------------------------------
       
   610 // Read the user snooze time setting for the active alarm.
       
   611 // ---------------------------------------------------------
       
   612 //
       
   613 void CAlarmUtils::GetSnoozeTimeSetting()
       
   614     {
       
   615     TRACE_ENTRY_POINT;
       
   616     // use the default value if cenrep reading fails
       
   617     iAlarmData.iSnoozeTime = KDefaultSnoozeTime;
       
   618 
       
   619     switch( iAlarmData.iAlarmType )
       
   620         {
       
   621         case EAlarmTypeClock:
       
   622             {
       
   623             CRepository* repository = NULL;
       
   624             PIM_TRAPD_ASSERT( repository = CRepository::NewL( KCRUidClockApp ); )
       
   625 
       
   626             if( repository )
       
   627                 {
       
   628                 repository->Get( KClockAppSnoozeTime, iAlarmData.iSnoozeTime );
       
   629                 delete repository;
       
   630                 }
       
   631             break;
       
   632             }
       
   633 
       
   634         case EAlarmTypeCalendar:
       
   635             {
       
   636             CRepository* repository = NULL;
       
   637             PIM_TRAPD_ASSERT( repository = CRepository::NewL( KCRUidCalendar ); )
       
   638 
       
   639             if( repository )
       
   640                 {
       
   641                 repository->Get( KCalendarSnoozeTime, iAlarmData.iSnoozeTime );
       
   642                 delete repository;
       
   643                 }
       
   644             break;
       
   645             }
       
   646 
       
   647         case EAlarmTypeOther:
       
   648             {
       
   649             iAlarmData.iSnoozeTime = KDefaultSnoozeTime;
       
   650             break;
       
   651             }
       
   652 
       
   653         default:
       
   654             ASSERT( EFalse );
       
   655         }
       
   656 
       
   657     TRACE_EXIT_POINT;
       
   658     }
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // Return ETrue if we are restoring data.
       
   662 // -----------------------------------------------------------------------------
       
   663 //
       
   664 TBool CAlarmUtils::IsPhoneInRestoreMode() const
       
   665     {
       
   666     TRACE_ENTRY_POINT;
       
   667     using namespace conn;
       
   668     TBool backupOrRestore( EFalse );
       
   669     TInt keyVal( 0 );
       
   670 
       
   671     // Get the back-up restore key
       
   672     const TInt error = RProperty::Get( KUidSystemCategory, conn::KUidBackupRestoreKey, keyVal );
       
   673     ASSERT( !error );
       
   674 
       
   675     if( !error )
       
   676         {
       
   677         // cast keyVal to TBURPartType
       
   678         const TBURPartType partType = static_cast< TBURPartType >( keyVal & KBURPartTypeMask );
       
   679 
       
   680         if( keyVal != 0 )
       
   681             {
       
   682             switch(partType)
       
   683                 {
       
   684                 case EBURRestoreFull:
       
   685                 case EBURRestorePartial:
       
   686                     backupOrRestore = ETrue;
       
   687                     break;
       
   688 
       
   689                 //case EBURUnset:
       
   690                 //case EBURNormal:
       
   691                 //case EBURBackupFull:
       
   692                 //case EBURBackupPartial:
       
   693                 default:
       
   694                     backupOrRestore = EFalse;
       
   695                     break;
       
   696                 }
       
   697             }
       
   698         }
       
   699     TRACE_EXIT_POINT;
       
   700     return backupOrRestore;
       
   701     }
       
   702 
       
   703 // -----------------------------------------------------------------------------
       
   704 // 
       
   705 // -----------------------------------------------------------------------------
       
   706 //
       
   707 void CAlarmUtils::GetAlarmLabelL(HBufC*& aText)
       
   708     {
       
   709     TRACE_ENTRY_POINT;
       
   710     iAlarmInfo->GetAlarmLabelL( iAlarmData.iAlarm,
       
   711                                 aText, 
       
   712                                 iAlarmData.iAlarmType );
       
   713     TRACE_EXIT_POINT;
       
   714     }
       
   715 
       
   716 // -----------------------------------------------------------------------------
       
   717 // 
       
   718 // -----------------------------------------------------------------------------
       
   719 //
       
   720 void CAlarmUtils::GetWakeupLabelL(HBufC*& aLabel)
       
   721     {
       
   722     TRACE_ENTRY_POINT;
       
   723     iAlarmInfo->GetWakeupLabelL( aLabel );
       
   724     TRACE_EXIT_POINT;
       
   725     }
       
   726 
       
   727 // -----------------------------------------------------------------------------
       
   728 // 
       
   729 // -----------------------------------------------------------------------------
       
   730 //
       
   731 CNotifierDialogController* CAlarmUtils::NotifierDialogController()
       
   732     {
       
   733     TRACE_ENTRY_POINT;
       
   734     TRACE_EXIT_POINT;
       
   735     return iNotifierDialogController;
       
   736     }
       
   737 
       
   738 // ---------------------------------------------------------
       
   739 // Checks if the alarm server has more expired alarms ready 
       
   740 // to be shown.
       
   741 // ---------------------------------------------------------
       
   742 //
       
   743 TBool CAlarmUtils::CheckHasExpiredAlarmsWaiting()
       
   744     {
       
   745     TRACE_ENTRY_POINT;
       
   746     TBool retVal( EFalse );
       
   747     const TInt err = iAlarmServer.Connect();
       
   748 
       
   749     if( !err )
       
   750         {
       
   751         RArray<TAlarmId> array;
       
   752         PIM_TRAPD_ASSERT( iAlarmServer.GetAlarmIdListByStateL( EAlarmStateWaitingToNotify, array ); )
       
   753         retVal = array.Count();
       
   754         array.Close();
       
   755         }
       
   756     iAlarmServer.Close();
       
   757     ASSERT( !err );
       
   758     TRACE_EXIT_POINT;
       
   759     return retVal;
       
   760     }
       
   761 
       
   762 // ---------------------------------------------------------
       
   763 // Update the alarm snooze count
       
   764 // ---------------------------------------------------------
       
   765 //
       
   766 void CAlarmUtils::UpdateAlarmSnoozeCount()
       
   767     {
       
   768     TRACE_ENTRY_POINT;
       
   769     TInt& count = iAlarmData.iAlarm.ClientData1();
       
   770 
       
   771     if( count >= KAlmAlertMinSnooze && 
       
   772         count <= KAlmAlertMaxSnooze )
       
   773         {
       
   774         count--;
       
   775         }
       
   776     else
       
   777         {
       
   778         count = KAlmAlertMaxSnooze;
       
   779         }
       
   780 
       
   781     const TInt err = iAlarmServer.Connect();
       
   782 
       
   783     if( !err )
       
   784         {
       
   785         iAlarmServer.SetClientData( iAlarmData.iAlarm );
       
   786         }
       
   787     iAlarmServer.Close();
       
   788     ASSERT( !err );
       
   789     TRACE_EXIT_POINT;
       
   790     }
       
   791 
       
   792 // ---------------------------------------------------------
       
   793 // Stop the active alarm and reset the snooze count.
       
   794 // ---------------------------------------------------------
       
   795 //
       
   796 void CAlarmUtils::DoStopAlarm()
       
   797     {
       
   798     TRACE_ENTRY_POINT;
       
   799     // reset the snooze count
       
   800     iAlarmData.iAlarm.ClientData1() = 0;
       
   801     const TInt err = iAlarmServer.Connect();
       
   802 
       
   803     if( !err )
       
   804         {
       
   805         iAlarmServer.SetClientData( iAlarmData.iAlarm );
       
   806         }
       
   807     iAlarmServer.Close();
       
   808     ASSERT( !err );
       
   809 
       
   810     // check if alarm server has more expired alarms waiting
       
   811     iHasExpiredAlarmsWaiting = CheckHasExpiredAlarmsWaiting();
       
   812 
       
   813     // stop the alarm
       
   814     iSupervisor->CmdAcknowledgeAlarm();
       
   815 
       
   816     #if defined( RD_ALMALERT__SENSOR_SUPPORT )
       
   817     // notify the result through the context framework
       
   818     if( iCFSupport )
       
   819         {
       
   820 	     if(!iAlarmControl->IsStopFromContext() && IsCalendarAlarm())
       
   821 	     	{
       
   822 	     	PIM_TRAPD_ASSERT( iCFSupport->PublishAlarmResultL( EResultAlarmStoppedAndExit );)
       
   823             iAlarmControl->SetStopFromContext(EFalse);
       
   824 	     	}
       
   825 	     else
       
   826 	     	{
       
   827 	     	PIM_TRAPD_ASSERT( iCFSupport->PublishAlarmResultL( EResultAlarmStopped );)
       
   828 	     	iAlarmControl->SetStopFromContext(EFalse);
       
   829 	     	}
       
   830         }
       
   831     #endif // RD_ALMALERT__SENSOR_SUPPORT
       
   832     TRACE_EXIT_POINT;
       
   833     }
       
   834 
       
   835 // ---------------------------------------------------------
       
   836 // 
       
   837 // ---------------------------------------------------------
       
   838 //
       
   839 TBool CAlarmUtils::HasExpiredAlarmsWaiting() const
       
   840     {
       
   841     TRACE_ENTRY_POINT;
       
   842     TRACE_EXIT_POINT;
       
   843     return iHasExpiredAlarmsWaiting;
       
   844     }
       
   845 
       
   846 // ---------------------------------------------------------
       
   847 // Snooze the alarm 
       
   848 // ---------------------------------------------------------
       
   849 //
       
   850 void CAlarmUtils::DoSnooze()
       
   851     {
       
   852     TRACE_ENTRY_POINT;
       
   853     /**
       
   854     * The snooze time can be shorter than iSnoozeTime because Alarm Server 
       
   855     * doesn't use seconds to count the new alarm time.  I.e. if the snooze time 
       
   856     * is 1 minute and 'snooze' is pressed at 12:05:50 the new alarm time will be 
       
   857     * 12:06:00.  So the actual snooze time will be only 10 seconds.  To prevent 
       
   858     * this kind of problems snooze time will be incremented by 1 minute when we
       
   859     * are less than 30 seconds away from the next minute.  The increment is done 
       
   860     * only when iSnoozeTime is 1.
       
   861     */
       
   862     TInt snoozeIncrement( 0 );
       
   863 
       
   864     if( iAlarmData.iSnoozeTime == 1 )
       
   865         {
       
   866         TTime now;  now.HomeTime();
       
   867         snoozeIncrement = now.DateTime().Second() > 30 ? 1 : 0;
       
   868         }
       
   869 
       
   870     // check if alarm server has more expired alarms waiting
       
   871     iHasExpiredAlarmsWaiting = CheckHasExpiredAlarmsWaiting();
       
   872 
       
   873     PIM_TRAPD_ASSERT( iSupervisor->CmdTaskAwayFromAlarmL( iAlarmData.iSnoozeTime + snoozeIncrement ); )
       
   874 
       
   875     #if defined( RD_ALMALERT__SENSOR_SUPPORT )
       
   876     // notify the result through the context framework
       
   877     if( iCFSupport )
       
   878         {
       
   879         PIM_TRAPD_ASSERT( iCFSupport->PublishAlarmResultL( EResultAlarmSnoozed ); )
       
   880         }
       
   881     #endif // RD_ALMALERT__SENSOR_SUPPORT
       
   882     TRACE_EXIT_POINT;
       
   883     }
       
   884 
       
   885 
       
   886 // ---------------------------------------------------------
       
   887 // Checks if this alarm can be shown in "alarm" or in 
       
   888 // "charging" mode.
       
   889 // ---------------------------------------------------------
       
   890 //
       
   891 TBool CAlarmUtils::IsWakeupAlarm() const
       
   892     {
       
   893     TRACE_ENTRY_POINT;    
       
   894     // has wakeup flag?
       
   895 	// SSM specific changes.
       
   896 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   897     const TBool retVal( iAlarmData.iAlarm.IsWakeup() );
       
   898 #else
       
   899     const TBool retVal( iAlarmData.iAlarm.ClientFlags().IsSet( KWakeupAlarmFlagIndex ) );
       
   900 #endif
       
   901     TRACE_EXIT_POINT;
       
   902     return retVal;
       
   903     }
       
   904 
       
   905 // ---------------------------------------------------------
       
   906 // 
       
   907 // ---------------------------------------------------------
       
   908 //
       
   909 TBool CAlarmUtils::IsDeviceInAlarmState() const
       
   910     {
       
   911     TRACE_ENTRY_POINT;    
       
   912     const TBool retVal( iCurrentDeviceState == ESwStateAlarm || 
       
   913                         iCurrentDeviceState == ESwStateCharging );    
       
   914     TRACE_EXIT_POINT;
       
   915     return retVal;
       
   916     }
       
   917 
       
   918 // ---------------------------------------------------------
       
   919 // 
       
   920 // ---------------------------------------------------------
       
   921 //
       
   922 TBool CAlarmUtils::IsDeviceInNormalState() const
       
   923     {
       
   924     TRACE_ENTRY_POINT;    
       
   925     const TBool retVal( iCurrentDeviceState == ESwStateNormalRfOn  || 
       
   926                         iCurrentDeviceState == ESwStateNormalRfOff || 
       
   927                         iCurrentDeviceState == ESwStateNormalBTSap );    
       
   928     TRACE_EXIT_POINT;
       
   929     return retVal;
       
   930     }
       
   931 
       
   932 // ---------------------------------------------------------
       
   933 // Check if the phone start-up is ready
       
   934 // ---------------------------------------------------------
       
   935 //
       
   936 TBool CAlarmUtils::IsPhoneStartupReady() const
       
   937     {
       
   938     TRACE_ENTRY_POINT;
       
   939     const TBool retVal( IsDeviceInAlarmState() ||
       
   940                         IsDeviceInNormalState() );
       
   941     TRACE_EXIT_POINT;
       
   942     return retVal;
       
   943     }
       
   944 
       
   945 // ---------------------------------------------------------
       
   946 // Set the iCurrentDeviceState
       
   947 // ---------------------------------------------------------
       
   948 //
       
   949 void CAlarmUtils::SetDeviceState(TPSGlobalSystemState aDeviceState)
       
   950     {
       
   951     TRACE_ENTRY_POINT;
       
   952     iCurrentDeviceState = aDeviceState;
       
   953     TRACE_EXIT_POINT;
       
   954     }
       
   955 
       
   956 // ---------------------------------------------------------
       
   957 // 
       
   958 // ---------------------------------------------------------
       
   959 //
       
   960 void CAlarmUtils::DeviceShutdown()
       
   961     {
       
   962     TRACE_ENTRY_POINT;
       
   963     iShutdownTimer->Cancel();
       
   964     if( StarterConnect() )
       
   965         {
       
   966         iStarter.Shutdown();
       
   967         iStarter.Close();
       
   968         }    
       
   969     TRACE_EXIT_POINT;
       
   970     }
       
   971 
       
   972 // ---------------------------------------------------------
       
   973 // 
       
   974 // ---------------------------------------------------------
       
   975 //
       
   976 void CAlarmUtils::DeviceStartup()
       
   977     {
       
   978     TRACE_ENTRY_POINT;    
       
   979     if( StarterConnect() )
       
   980         {
       
   981         PIM_ASSERT( iStarter.SetState( RStarterSession::ENormal ); )
       
   982         iStarter.Close();
       
   983         }  
       
   984     TRACE_EXIT_POINT;
       
   985     }
       
   986 
       
   987 // ---------------------------------------------------------
       
   988 // Connect to the starter.
       
   989 // ---------------------------------------------------------
       
   990 TBool CAlarmUtils::StarterConnect()
       
   991     {
       
   992     TRACE_ENTRY_POINT;
       
   993     TInt err( KErrNone );    
       
   994     /**
       
   995     * Starter session's Connect() call can NOT be inside any 
       
   996     * function called during the class construction!
       
   997     * (NewL, ConstructL and all the functions called by these functions)
       
   998     * Calling Connect() during construction causes a deadlock!
       
   999     */
       
  1000     err = iStarter.Connect();
       
  1001     ASSERT( !err );    
       
  1002     TRACE_EXIT_POINT;
       
  1003     return( err == KErrNone );
       
  1004     }
       
  1005 
       
  1006 // ---------------------------------------------------------
       
  1007 // (re)start the shutdown timer.
       
  1008 // ---------------------------------------------------------
       
  1009 //
       
  1010 void CAlarmUtils::StartShutdownTimer(TUint aTimeout)
       
  1011     {
       
  1012     TRACE_ENTRY_POINT;
       
  1013     // shutdown allowed only if the device is in alarm state
       
  1014     if( IsDeviceInAlarmState() )
       
  1015         {
       
  1016         iShutdownTimer->Cancel();
       
  1017         TCallBack shutdownCallback( ShutdownCallBack, this );
       
  1018         iShutdownTimer->Start(
       
  1019             TTimeIntervalMicroSeconds32( aTimeout ),
       
  1020             TTimeIntervalMicroSeconds32( aTimeout ),
       
  1021             shutdownCallback );
       
  1022         }
       
  1023     TRACE_EXIT_POINT;
       
  1024     }
       
  1025 
       
  1026 // ---------------------------------------------------------
       
  1027 // Callback for the shutdown timer
       
  1028 // ---------------------------------------------------------
       
  1029 //
       
  1030 TInt CAlarmUtils::ShutdownCallBack(TAny* aPtr)
       
  1031     {
       
  1032     TRACE_ENTRY_POINT;
       
  1033     static_cast<CAlarmUtils*>( aPtr )->DeviceShutdown();
       
  1034     TRACE_EXIT_POINT;
       
  1035     return 0;
       
  1036     }
       
  1037 
       
  1038 // ---------------------------------------------------------
       
  1039 // Cancel the shutdown timer
       
  1040 // ---------------------------------------------------------
       
  1041 //
       
  1042 void CAlarmUtils::CancelShutdown()
       
  1043     {
       
  1044     TRACE_ENTRY_POINT;
       
  1045     iShutdownTimer->Cancel();
       
  1046     TRACE_EXIT_POINT;
       
  1047     }
       
  1048 
       
  1049 // ---------------------------------------------------------
       
  1050 // (re)start the auto-snooze timer.
       
  1051 // ---------------------------------------------------------
       
  1052 //
       
  1053 void CAlarmUtils::StartAutoSnoozeTimer()
       
  1054     {
       
  1055     TRACE_ENTRY_POINT;
       
  1056     iAutoSnoozeTimer->Cancel();
       
  1057     TCallBack autoSnoozeCallback( AutoSnoozeCallBack, this );
       
  1058     TInt autoSnoozeTime;
       
  1059 	if(IsCalendarAlarm())
       
  1060 		{
       
  1061 		autoSnoozeTime = KAlarmAutoHideCalendar; // 30 Sec
       
  1062 		}
       
  1063 	else
       
  1064 		{
       
  1065 		autoSnoozeTime = KAlarmAutoHide; // 60 Sec
       
  1066 		}
       
  1067 	iAutoSnoozeTimer->Start(
       
  1068         TTimeIntervalMicroSeconds32( autoSnoozeTime ),
       
  1069         TTimeIntervalMicroSeconds32( autoSnoozeTime ),
       
  1070         autoSnoozeCallback );
       
  1071     TRACE_EXIT_POINT;
       
  1072     }
       
  1073 
       
  1074 // ---------------------------------------------------------
       
  1075 // Callback for the auto-snooze timer.
       
  1076 // ---------------------------------------------------------
       
  1077 //
       
  1078 TInt CAlarmUtils::AutoSnoozeCallBack(TAny* aPtr)
       
  1079     {
       
  1080     TRACE_ENTRY_POINT;
       
  1081     static_cast<CAlarmUtils*>( aPtr )->AutoSnooze();
       
  1082     TRACE_EXIT_POINT;
       
  1083     return 0;
       
  1084     }
       
  1085 
       
  1086 // ---------------------------------------------------------
       
  1087 // 
       
  1088 // ---------------------------------------------------------
       
  1089 //
       
  1090 void CAlarmUtils::AutoSnooze()
       
  1091     {
       
  1092     TRACE_ENTRY_POINT;
       
  1093     iAutoSnoozeTimer->Cancel();
       
  1094     iAlarmControl->DoAutoSnooze();
       
  1095     TRACE_EXIT_POINT;
       
  1096     }
       
  1097 
       
  1098 // ---------------------------------------------------------
       
  1099 // Cancel the snooze timer
       
  1100 // ---------------------------------------------------------
       
  1101 //
       
  1102 void CAlarmUtils::CancelAutoSnooze()
       
  1103     {
       
  1104     TRACE_ENTRY_POINT;
       
  1105     iAutoSnoozeTimer->Cancel();
       
  1106     TRACE_EXIT_POINT;
       
  1107     }
       
  1108 
       
  1109 // ---------------------------------------------------------
       
  1110 // 
       
  1111 // ---------------------------------------------------------
       
  1112 //
       
  1113 void CAlarmUtils::StartAccessoryObserver()
       
  1114     {
       
  1115     TRACE_ENTRY_POINT;    
       
  1116     PIM_TRAPD_ASSERT( iRemConHandler->StartL(); )    
       
  1117     TRACE_EXIT_POINT;
       
  1118     }
       
  1119 
       
  1120 // ---------------------------------------------------------
       
  1121 // 
       
  1122 // ---------------------------------------------------------
       
  1123 //
       
  1124 void CAlarmUtils::StopAccessoryObserver()
       
  1125     {
       
  1126     TRACE_ENTRY_POINT;    
       
  1127     iRemConHandler->Stop();    
       
  1128     TRACE_EXIT_POINT;
       
  1129     }
       
  1130 
       
  1131 // ---------------------------------------------------------
       
  1132 // Publish new alarm context value and wait for any actions
       
  1133 // ---------------------------------------------------------
       
  1134 //
       
  1135 void CAlarmUtils::StartCFObserverL()
       
  1136     {
       
  1137     TRACE_ENTRY_POINT;
       
  1138     #ifdef RD_ALMALERT__SENSOR_SUPPORT
       
  1139     if( !iCFSupport )
       
  1140         {
       
  1141         /**
       
  1142         * NOTE!
       
  1143         * We shouldn't create the CCFClient before the 
       
  1144         * CF server has been started by the starter. 
       
  1145         */
       
  1146         iCFSupport = CAlarmContextFwSupport::NewL( iAlarmControl );
       
  1147         }
       
  1148     iCFSupport->StartL( IsCalendarAlarm() ? EStateCalendarAlarm : 
       
  1149                             IsClockAlarm() ? EStateClockAlarm : 
       
  1150                                 EStateOtherAlarm );
       
  1151     #endif // RD_ALMALERT__SENSOR_SUPPORT
       
  1152     TRACE_EXIT_POINT;
       
  1153     }
       
  1154 
       
  1155 // ---------------------------------------------------------
       
  1156 // Stop observing context framework/sensors.
       
  1157 // ---------------------------------------------------------
       
  1158 //
       
  1159 void CAlarmUtils::StopCFObserverL()
       
  1160     {
       
  1161     TRACE_ENTRY_POINT;
       
  1162     #ifdef RD_ALMALERT__SENSOR_SUPPORT
       
  1163     if( iCFSupport )
       
  1164         {
       
  1165         iCFSupport->StopL();
       
  1166         }
       
  1167     #endif // RD_ALMALERT__SENSOR_SUPPORT
       
  1168     TRACE_EXIT_POINT;
       
  1169     }
       
  1170 
       
  1171 // ---------------------------------------------------------
       
  1172 // Try to snooze the active alarm.
       
  1173 // ---------------------------------------------------------
       
  1174 //
       
  1175 void CAlarmUtils::TryToSnoozeActiveAlarm()
       
  1176     {
       
  1177     TRACE_ENTRY_POINT;
       
  1178     iAutoSnoozeTimer->Cancel();
       
  1179 
       
  1180     if( CanSnooze() )
       
  1181         {
       
  1182         UpdateAlarmSnoozeCount();
       
  1183         DoSnooze();
       
  1184         }
       
  1185     else // max snooze count reached >> stop the alarm
       
  1186         {
       
  1187         DoStopAlarm();
       
  1188         }
       
  1189     TRACE_EXIT_POINT;
       
  1190     }
       
  1191 
       
  1192 // ---------------------------------------------------------
       
  1193 // Ignore all key presses for [KKeyBlockTime] seconds.
       
  1194 // ---------------------------------------------------------
       
  1195 //
       
  1196 void CAlarmUtils::StartKeyBlocker()
       
  1197     {
       
  1198     TRACE_ENTRY_POINT;
       
  1199     iKeyBlockTimer->Cancel();
       
  1200     TCallBack keyBlockCallback( KeyBlockCallBack, iKeyBlockTimer );
       
  1201     iKeyBlockTimer->Start(
       
  1202         TTimeIntervalMicroSeconds32( KKeyBlockTime ),
       
  1203         TTimeIntervalMicroSeconds32( KKeyBlockTime ),
       
  1204         keyBlockCallback );
       
  1205     TRACE_EXIT_POINT;
       
  1206     }
       
  1207 
       
  1208 // ---------------------------------------------------------
       
  1209 // Callback function for the key block timer
       
  1210 // ---------------------------------------------------------
       
  1211 //
       
  1212 TInt CAlarmUtils::KeyBlockCallBack(TAny* aPtr)
       
  1213     {
       
  1214     TRACE_ENTRY_POINT;
       
  1215     static_cast<CPeriodic*>( aPtr )->Cancel();
       
  1216     TRACE_EXIT_POINT;
       
  1217     return 0;
       
  1218     }
       
  1219 
       
  1220 // ---------------------------------------------------------
       
  1221 // 
       
  1222 // ---------------------------------------------------------
       
  1223 //
       
  1224 TBool CAlarmUtils::KeyBlockActive()
       
  1225     {
       
  1226     TRACE_ENTRY_POINT;
       
  1227     const TBool retVal( iKeyBlockTimer->IsActive() );
       
  1228     TRACE_EXIT_POINT;
       
  1229     return retVal;
       
  1230     }
       
  1231 
       
  1232 // ---------------------------------------------------------
       
  1233 // 
       
  1234 // ---------------------------------------------------------
       
  1235 //
       
  1236 void CAlarmUtils::AsyncShowSnoozeInfoNote()
       
  1237     {
       
  1238     TRACE_ENTRY_POINT;
       
  1239     iAsyncCallBack->Cancel();
       
  1240     iAsyncCallBack->Set( TCallBack( SnoozeInfoCallBack, iAlarmControl ) );
       
  1241     iAsyncCallBack->CallBack();
       
  1242     TRACE_EXIT_POINT;
       
  1243     }
       
  1244 
       
  1245 // ---------------------------------------------------------
       
  1246 // Callback function for the auto snooze timer
       
  1247 // ---------------------------------------------------------
       
  1248 //
       
  1249 TInt CAlarmUtils::SnoozeInfoCallBack(TAny* aPtr)
       
  1250     {
       
  1251     TRACE_ENTRY_POINT;
       
  1252     PIM_TRAPD_ASSERT( static_cast<CAlmAlarmControl*>( aPtr )->ShowSnoozeInfoNoteL(); )
       
  1253     TRACE_EXIT_POINT;
       
  1254     return 0;
       
  1255     }
       
  1256 
       
  1257 // ---------------------------------------------------------
       
  1258 // 
       
  1259 // ---------------------------------------------------------
       
  1260 //
       
  1261 void CAlarmUtils::AsyncShowWakeupQuery()
       
  1262     {
       
  1263     TRACE_ENTRY_POINT;
       
  1264     iAsyncCallBack->Cancel();
       
  1265     iAsyncCallBack->Set( TCallBack( WakeupCallBack, this ) );
       
  1266     iAsyncCallBack->CallBack();
       
  1267     TRACE_EXIT_POINT;
       
  1268     }
       
  1269 
       
  1270 // ---------------------------------------------------------
       
  1271 // 
       
  1272 // ---------------------------------------------------------
       
  1273 //
       
  1274 TInt CAlarmUtils::WakeupCallBack(TAny* aPtr)
       
  1275     {
       
  1276     TRACE_ENTRY_POINT;
       
  1277     static_cast<CAlarmUtils*>( aPtr )->DoAskWakeup();
       
  1278     TRACE_EXIT_POINT;
       
  1279     return 0;
       
  1280     }
       
  1281 
       
  1282 // ---------------------------------------------------------
       
  1283 // Request wakeup dialog. Shutdown on error.
       
  1284 // ---------------------------------------------------------
       
  1285 //
       
  1286 void CAlarmUtils::DoAskWakeup()
       
  1287     {
       
  1288     TRACE_ENTRY_POINT;
       
  1289     TRAPD( err, iAlarmControl->AskWakeupPhoneL(); )
       
  1290     ASSERT( !err );
       
  1291 
       
  1292     if( err ) // shutdown if the query failed
       
  1293         {
       
  1294         StartShutdownTimer( KShutdownTime );
       
  1295         }
       
  1296     TRACE_EXIT_POINT;
       
  1297     }
       
  1298 
       
  1299 // ---------------------------------------------------------
       
  1300 // 
       
  1301 // ---------------------------------------------------------
       
  1302 //
       
  1303 void CAlarmUtils::CancelAsynchRequest()
       
  1304     {
       
  1305     TRACE_ENTRY_POINT;
       
  1306     iAsyncCallBack->Cancel();
       
  1307     TRACE_EXIT_POINT;
       
  1308     }
       
  1309 
       
  1310 // ---------------------------------------------------------
       
  1311 // Delay alarm for KAlarmDelayTime
       
  1312 // ---------------------------------------------------------
       
  1313 //
       
  1314 void CAlarmUtils::StartAlarmDelayTimer()
       
  1315     {
       
  1316     TRACE_ENTRY_POINT;
       
  1317     iAlarmDelayTimer->Cancel();
       
  1318     TCallBack delayTimerCallback( DelayTimerCallBack, this );
       
  1319     iAlarmDelayTimer->Start(
       
  1320         TTimeIntervalMicroSeconds32( KAlarmDelayTime ),
       
  1321         TTimeIntervalMicroSeconds32( KAlarmDelayTime ),
       
  1322         delayTimerCallback );
       
  1323     TRACE_EXIT_POINT;
       
  1324     }
       
  1325 
       
  1326 // ---------------------------------------------------------
       
  1327 // Callback function for the key block timer
       
  1328 // ---------------------------------------------------------
       
  1329 //
       
  1330 TInt CAlarmUtils::DelayTimerCallBack(TAny* aPtr)
       
  1331     {
       
  1332     TRACE_ENTRY_POINT;
       
  1333     CAlarmUtils* alarmUtils = static_cast<CAlarmUtils*>( aPtr );
       
  1334     alarmUtils->iAlarmDelayTimer->Cancel();
       
  1335     alarmUtils->iAlarmControl->ShowAlarm();
       
  1336     TRACE_EXIT_POINT;
       
  1337     return 0;
       
  1338     }
       
  1339 
       
  1340 // ---------------------------------------------------------
       
  1341 // Reset system inactivity timers periodically.
       
  1342 // ---------------------------------------------------------
       
  1343 //
       
  1344 void CAlarmUtils::StartInactivityResetTimer()
       
  1345     {
       
  1346     TRACE_ENTRY_POINT;
       
  1347     User::ResetInactivityTime();
       
  1348     iInactivityResetTimer->Cancel();
       
  1349     TCallBack inactivityResetCallback( InactivityResetTimerCallBack, this );
       
  1350     iInactivityResetTimer->Start(
       
  1351         TTimeIntervalMicroSeconds32( KInactivityResetInterval ),
       
  1352         TTimeIntervalMicroSeconds32( KInactivityResetInterval ),
       
  1353         inactivityResetCallback );
       
  1354     TRACE_EXIT_POINT;
       
  1355     }
       
  1356 
       
  1357 // ---------------------------------------------------------
       
  1358 // Callback function for the inactivity reset timer.
       
  1359 // ---------------------------------------------------------
       
  1360 //
       
  1361 TInt CAlarmUtils::InactivityResetTimerCallBack(TAny* /*aPtr*/)
       
  1362     {
       
  1363     TRACE_ENTRY_POINT;
       
  1364     User::ResetInactivityTime();
       
  1365     TRACE_EXIT_POINT;
       
  1366     return 1;
       
  1367     }
       
  1368 
       
  1369 // ---------------------------------------------------------
       
  1370 // Reset system inactivity timers periodically.
       
  1371 // ---------------------------------------------------------
       
  1372 //
       
  1373 void CAlarmUtils::StopInactivityResetTimer()
       
  1374     {
       
  1375     TRACE_ENTRY_POINT;
       
  1376     iInactivityResetTimer->Cancel();
       
  1377     TRACE_EXIT_POINT;
       
  1378     }
       
  1379 
       
  1380 // ---------------------------------------------------------
       
  1381 // 
       
  1382 // ---------------------------------------------------------
       
  1383 //
       
  1384 TBool CAlarmUtils::AlarmDelayTimerActive()
       
  1385     {
       
  1386     TRACE_ENTRY_POINT;
       
  1387     const TBool retVal( iAlarmDelayTimer->IsActive() );
       
  1388     TRACE_EXIT_POINT;
       
  1389     return retVal;
       
  1390     }
       
  1391 
       
  1392 // ---------------------------------------------------------
       
  1393 // Fetch local uid and instance time for the alarmed calendar entry.
       
  1394 // ---------------------------------------------------------
       
  1395 //
       
  1396 void CAlarmUtils::GetCalendarEntryInfoL()
       
  1397     {
       
  1398     TRACE_ENTRY_POINT;
       
  1399     iAlarmData.iInstanceTime = TCalTime();
       
  1400     iAlarmData.iLocalUid = 0;
       
  1401 
       
  1402     if( !IsCalendarAlarm() )
       
  1403         {
       
  1404         TRACE_EXIT_POINT;
       
  1405         return;
       
  1406         }
       
  1407     
       
  1408     TInt err = KErrNone;
       
  1409     
       
  1410     //Retriving alarm data from alarm server.
       
  1411     err = iAlarmServer.Connect();
       
  1412     User::LeaveIfError( err );
       
  1413     HBufC8* alarmData = NULL;
       
  1414     err = iAlarmServer.GetAlarmData( iAlarmData.iAlarm.Id(), alarmData );
       
  1415     CleanupStack::PushL( alarmData );
       
  1416     iAlarmServer.Close();
       
  1417     User::LeaveIfError( err );
       
  1418     
       
  1419     // get entry data
       
  1420     CCalEntryId* entryId = CCalEntryId::NewL( alarmData );
       
  1421     CleanupStack::Pop( alarmData ); // ownership transferred to CCalEntryId
       
  1422     CleanupStack::PushL( entryId );
       
  1423 
       
  1424     // instance time
       
  1425     iAlarmData.iInstanceTime = entryId->InstanceTimeL();
       
  1426 
       
  1427     // local UID
       
  1428     iAlarmData.iLocalUid = entryId->LocalUidL();
       
  1429 	
       
  1430 	// Get the calendar db name.
       
  1431 	iAlarmData.iCalFileName = entryId->StoreFileNameL();
       
  1432 
       
  1433 	// Creating session with Calendar server
       
  1434 	if(!iCalSession)
       
  1435 		{
       
  1436 		iCalSession = CCalSession::NewL();
       
  1437 		TRAP( err, iCalSession->OpenL( iAlarmData.iCalFileName ) );
       
  1438 		User::LeaveIfError( err );
       
  1439 
       
  1440 		// active scheduler wait object to wait for Calendar view creation
       
  1441 		iEntryAsyncWait = new(ELeave) CActiveSchedulerWait();
       
  1442 		
       
  1443 		// active scheduler wait object to stop new view creation req. while one is ongoing.
       
  1444 		iQueueManagerAsw = new(ELeave) CActiveSchedulerWait();
       
  1445 		}
       
  1446 
       
  1447 	if(!iCalEntryView && !(iEntryAsyncWait->IsStarted()) && iCalSession)
       
  1448 		{
       
  1449 		
       
  1450 		// Creating Calendar Entry View for retrieving event details.
       
  1451 	    iCalEntryView = CCalEntryView::NewL( *iCalSession, *this );
       
  1452 		SetCalendarViewStatus(ECalViewCreateReqPending);
       
  1453 		
       
  1454 		// Since View creation is aynshronous,the thread need to wait till view creation completed
       
  1455 		iEntryAsyncWait->Start();
       
  1456 
       
  1457 		// If Async wait  queue manager is still in in waiting,Cancel it since view creation completed.
       
  1458 		if(iQueueManagerAsw->IsStarted())
       
  1459 			{
       
  1460 			iQueueManagerAsw->AsyncStop();
       
  1461 			}
       
  1462 		
       
  1463 		}
       
  1464 	else
       
  1465 		{	
       
  1466 		// Waiting for completion of current view creation request.
       
  1467 		if(iQueueManagerAsw && !iQueueManagerAsw->IsStarted())
       
  1468 			{
       
  1469 			iQueueManagerAsw->Start();
       
  1470 			}
       
  1471 		}
       
  1472 	// Leaves if view creation failed...
       
  1473 	User::LeaveIfError(CalendarViewStatus());
       
  1474 	
       
  1475 	CleanupStack::PopAndDestroy( entryId );
       
  1476     
       
  1477     TRACE_EXIT_POINT;
       
  1478     }
       
  1479 
       
  1480 // ----------------------------------------------------------------------------
       
  1481 // Fetch the alarmed entry from calendar database
       
  1482 // ----------------------------------------------------------------------------
       
  1483 //
       
  1484 void CAlarmUtils::FetchEntryL(CCalEntry*& aEntry)
       
  1485     {
       
  1486     TRACE_ENTRY_POINT;
       
  1487 	if(CalendarViewStatus()== ECalViewCreateCompleted&& NULL!=iCalEntryView)
       
  1488 		{
       
  1489 		aEntry = iCalEntryView->FetchL( iAlarmData.iLocalUid );
       
  1490 		User::LeaveIfNull(aEntry);
       
  1491 		}
       
  1492 	else
       
  1493 		{
       
  1494 		// leaving if Calender Entry view is not available.
       
  1495 		User::Leave(KErrNotFound);
       
  1496 		}
       
  1497 
       
  1498     if( aEntry )
       
  1499         {
       
  1500         // check if the entry still has an alarm
       
  1501         // (prevent deleted snoozed alarms)
       
  1502         //
       
  1503         CCalAlarm* alarm = NULL;
       
  1504         TRAPD( err, alarm = aEntry->AlarmL(); )
       
  1505 
       
  1506         // ignore and stop the alarm (handled in CAlmAlarmControl) if
       
  1507         // the alarm was removed from the calendar entry
       
  1508         if( !alarm || err != KErrNone )
       
  1509             {
       
  1510             delete aEntry;
       
  1511             aEntry = NULL;
       
  1512             }
       
  1513         delete alarm;
       
  1514         }
       
  1515 
       
  1516     if( !aEntry )
       
  1517         {
       
  1518         // calendar entry not found...stop the alarm
       
  1519         User::Leave( KErrNotFound );  // trapped in CAlmAlarmControl
       
  1520         }
       
  1521     TRACE_EXIT_POINT;
       
  1522     }
       
  1523 
       
  1524 
       
  1525 // ----------------------------------------------------------------------------
       
  1526 //From MCalProgressCallBack . Notifies the progress percentage of  completion of 
       
  1527 //calendar entry view creation
       
  1528 // ----------------------------------------------------------------------------
       
  1529 //
       
  1530 
       
  1531 void CAlarmUtils::Progress(TInt /* aPercentageCompleted */)
       
  1532 {
       
  1533     TRACE_ENTRY_POINT;
       
  1534 	// Do Nothing
       
  1535 	TRACE_EXIT_POINT;
       
  1536 
       
  1537 }
       
  1538 
       
  1539 // ----------------------------------------------------------------------------
       
  1540 //From MCalProgressCallBack . Called  on completion of calendar entry view creation
       
  1541 // ----------------------------------------------------------------------------
       
  1542 //
       
  1543 void CAlarmUtils::Completed(TInt aError)
       
  1544 	{
       
  1545 	TRACE_ENTRY_POINT;
       
  1546 
       
  1547 	if(iEntryAsyncWait->IsStarted())
       
  1548 	{
       
  1549 		iEntryAsyncWait->AsyncStop();
       
  1550 	}
       
  1551 
       
  1552 	if(iQueueManagerAsw->IsStarted())
       
  1553 	{
       
  1554 		iQueueManagerAsw->AsyncStop();
       
  1555 	}
       
  1556 		
       
  1557 	
       
  1558 	if(KErrNone == aError)
       
  1559 	{
       
  1560 		SetCalendarViewStatus(ECalViewCreateCompleted);
       
  1561 	}
       
  1562 	else
       
  1563 	{
       
  1564 		SetCalendarViewStatus(ECalViewCreateError);
       
  1565 	}
       
  1566 
       
  1567 	TRACE_EXIT_POINT;
       
  1568 	}
       
  1569 
       
  1570 
       
  1571 // ----------------------------------------------------------------------------
       
  1572 //From MCalProgressCallBack . Called  on compltetion of calendar entry view creation
       
  1573 // ----------------------------------------------------------------------------
       
  1574 //
       
  1575 
       
  1576 TBool CAlarmUtils::NotifyProgress()
       
  1577 	{
       
  1578     TRACE_ENTRY_POINT;
       
  1579 	// Don nothing
       
  1580 	TRACE_EXIT_POINT;
       
  1581 	return EFalse;
       
  1582 
       
  1583 	}
       
  1584 
       
  1585 
       
  1586 // ----------------------------------------------------------------------------
       
  1587 // Sets the Calendar view creation status.
       
  1588 //
       
  1589 void CAlarmUtils::SetCalendarViewStatus(TInt aStatus)
       
  1590 	{
       
  1591 	TRACE_ENTRY_POINT;
       
  1592 
       
  1593 	iCalEntryViewStatus = aStatus ;
       
  1594 	
       
  1595 	TRACE_EXIT_POINT;
       
  1596 	}
       
  1597 
       
  1598 // ----------------------------------------------------------------------------
       
  1599 //Gets the Calendar view creation status.
       
  1600 // ----------------------------------------------------------------------------
       
  1601 TInt CAlarmUtils::CalendarViewStatus()
       
  1602 	{
       
  1603 	TRACE_ENTRY_POINT;	
       
  1604 	TRACE_EXIT_POINT;
       
  1605 	return iCalEntryViewStatus;
       
  1606 	}
       
  1607 
       
  1608 // ---------------------------------------------------------
       
  1609 // Launch calendar into alarm viewer mode.
       
  1610 // ---------------------------------------------------------
       
  1611 //
       
  1612 void CAlarmUtils::StartCalendarL()
       
  1613     {
       
  1614     TRACE_ENTRY_POINT;
       
  1615     
       
  1616     iCalendarAlarmViewer = ETrue;
       
  1617     
       
  1618     CalenLauncher::ViewEntryL( iAlarmData.iLocalUid,
       
  1619                                iAlarmData.iInstanceTime, iAlarmData.iCalFileName,
       
  1620                                CanSnooze() ? CalenLauncher::EAlarmViewer : 
       
  1621                                              CalenLauncher::EAlarmViewerNoSnooze );
       
  1622     TRACE_EXIT_POINT;
       
  1623     }
       
  1624 
       
  1625 // ---------------------------------------------------------
       
  1626 // Check if the security lock is active.
       
  1627 // ---------------------------------------------------------
       
  1628 //
       
  1629 TBool CAlarmUtils::IsSecurityLockActive()
       
  1630     {
       
  1631     TRACE_ENTRY_POINT;
       
  1632     TInt keyVal( 0 );
       
  1633     TBool retVal( EFalse );
       
  1634 
       
  1635     PIM_ASSERT( RProperty::Get( KPSUidCoreApplicationUIs, KCoreAppUIsAutolockStatus, keyVal ); )
       
  1636     if( keyVal > EAutolockOff )
       
  1637         {
       
  1638         retVal = ETrue;
       
  1639         }
       
  1640     TRACE_EXIT_POINT;
       
  1641     return retVal;
       
  1642     }
       
  1643 
       
  1644 // ---------------------------------------------------------
       
  1645 // Set the status for calendar alarm viewer.
       
  1646 // ---------------------------------------------------------
       
  1647 //    
       
  1648 void CAlarmUtils::SetCalendarAlarmViewer(TBool aCalendarAlarmViewer)
       
  1649     {
       
  1650     iCalendarAlarmViewer = aCalendarAlarmViewer ;
       
  1651     }
       
  1652 
       
  1653 // ---------------------------------------------------------
       
  1654 // Get the status whether calendar viewer is open or not.
       
  1655 // ---------------------------------------------------------
       
  1656 //
       
  1657 TBool CAlarmUtils::IsCalendarAlarmViewer()
       
  1658     {
       
  1659     return iCalendarAlarmViewer;
       
  1660     }
       
  1661 
       
  1662 // End of File