void CCalAlarmAttachTest::ConstructL()
	{
	iTestLib = CCalTestLibrary::NewL();
	iTestLib->ReplaceFileL(KCalName());
	iTestLib->OpenFileL(KCalName());
	
	User::LeaveIfError(iAlarmServer.Connect());
	}
TInt CCalAlarmAttachTest::CountAlarmsL(TAlarmId& aFirstAlarmId)
	{
	RArray< TAlarmId > alarmIdArr;
	
	iAlarmServer.GetAlarmIdListL(alarmIdArr);
	
	const TInt KCount = alarmIdArr.Count();
	if (KCount > 0)
		{
		aFirstAlarmId = alarmIdArr[0];
		}
	TASShdAlarm alarmDetails;
	for (TInt i = 0; i < KCount; i++)
		{
		User::LeaveIfError(iAlarmServer.GetAlarmDetails(alarmIdArr[i], alarmDetails));
		RDebug::Print(_L("alarm %d, state=%d, id=%d"), i, alarmDetails.State(), alarmIdArr[i]);
		}
	RDebug::Print(_L("alarm count: %d"), KCount);
	alarmIdArr.Close();
	
	return KCount;
	}
void CCalAlarmAttachTest::ClearAllAlarmsL()
	{
	iTestLib->CleanDatabaseL();
	
	// Delete all alarms
	RArray<TAlarmCategory> categories;
	CleanupClosePushL(categories);
	
	iAlarmServer.GetAvailableCategoryListL(categories);
	
	TInt count = categories.Count();
	for(TInt i = 0; i < count; i++)
		{
		const TAlarmCategory& category = categories[i];
		test(iAlarmServer.AlarmDeleteAllByCategory(category, EFalse) == KErrNone);
		}
		
	CleanupStack::PopAndDestroy(&categories);

	// Restore alarm sound
	iAlarmServer.SetAlarmSoundState(EAlarmGlobalSoundStateOn);
	}
void CCalAlarmAttachTest::TestDeleteAlarmL()
	{
	
	CCalEntryView &view = iTestLib->SynCGetEntryViewL() ;
	CCalEntryView *calentryview = &view ;
	
	TInt num ;	

	ClearAllAlarmsL() ;	
	
	test.Printf(_L("Going to check deletion of non-snoozed alarms\n")) ;
	
	/* create repeating parent entry	*/

	HBufC8* guid = KGUID3().AllocLC(); // ownership of guid gets transferred
 	CCalEntry *entry = CCalEntry::NewL(CCalEntry::ETodo, guid, CCalEntry::EMethodNone, 0) ;
	CleanupStack::Pop(guid);	
	CleanupStack::PushL(entry) ;
	
	TTime now1;
	now1.UniversalTime();
	now1 += TTimeIntervalSeconds(30);

	TCalTime startTime ;
	TCalTime endTime ;	
	startTime.SetTimeUtcL(now1) ;
	endTime.SetTimeUtcL(now1 + TTimeIntervalSeconds(30)) ;
			
	entry->SetStartAndEndTimeL(startTime, endTime) ;

	TCalRRule rrule(TCalRRule::EDaily) ;
	rrule.SetDtStart(startTime);
	
	TCalTime endRTime ;		
	endRTime.SetTimeUtcL(now1 + TTimeIntervalDays(7)) ;	
	rrule.SetUntil(endRTime);
	rrule.SetInterval(1);	
	entry->SetRRuleL(rrule) ;

	CCalAlarm *alarm = CCalAlarm::NewL() ;
	CleanupStack::PushL(alarm) ;			
	alarm->SetTimeOffset(TTimeIntervalMinutes(0)) ;
	entry->SetAlarmL(alarm) ; //ownership does not get transferred
	
	RPointerArray<CCalEntry> entryarr ;		
	entryarr.AppendL(entry) ; //ownership does not got transferred			
	calentryview->StoreL(entryarr, num) ;
	entryarr.Close() ;		
		
	CleanupStack::PopAndDestroy(alarm) ;	
	CleanupStack::PopAndDestroy(entry) ;	
	
	/* Now let the alarm go off */
	
	RArray< TAlarmId > alarmIdArr ;
	
	iAlarmServer.GetAlarmIdListL(alarmIdArr) ;
	
	TInt cnt = alarmIdArr.Count() ;
	
	test(cnt > 0) ;
	
	TRequestStatus status = 0;
	TAlarmId alarmId = alarmIdArr[0];
	
	alarmIdArr.Close() ;
	
	test.Printf(_L("Waiting one minute for the alarm to go off...\n")) ;
	
	for(;;)
		{
		iAlarmServer.NotifyChange(status, alarmId);
		User::WaitForRequest(status);
		
		PrintEventDetails(status.Int());
		
		if	(status.Int() == EAlarmChangeEventTimerExpired)
			{			
			break; // alarm expired
			}
		}

	/* delete this entry - without snoozing */

	RPointerArray< CCalEntry > calEntryArray ;
	calentryview->FetchL(KGUID3(), calEntryArray) ;
	
	test(calEntryArray.Count() == 1) ;
	
	CCalEntry *entry1 = calEntryArray[0] ;		
	
	entry1->SetCompletedL(ETrue, startTime) ;
		
	calentryview->StoreL(calEntryArray, num) ;
	
	delete entry1 ;	
	calEntryArray.Close() ;
	
	/* now check if the alarm still exists or not */
	
	RArray< TAlarmId > alarmIdArr1 ;
	
	iAlarmServer.GetAlarmIdListL(alarmIdArr1) ;
	
	TInt cnt1 = alarmIdArr1.Count() ;
	
	test(cnt1 < cnt) ;		
	
	if (cnt1 > 0)
		{
		TAlarmId alarmId1 = alarmIdArr1[0];	
		test(alarmId != alarmId1) ;
		}			
	
	alarmIdArr1.Close() ;	
	
	ClearAllAlarmsL() ;
	
	test.Printf(_L("Checking of non snoozed alarms successful\n")) ;
	
	}
/**
 * @brief Completes the second phase of Symbian object construction.
 * Put initialization code that could leave here.
 */
void CAlarmSwitcherAppUi::ConstructL()
{
    // [[[ begin generated region: do not modify [Generated Contents]

    BaseConstructL( EAknEnableSkin  |
                    EAknEnableMSK );
    InitializeContainersL();
    // ]]] end generated region [Generated Contents]

    RASCliSession session;
    session.Connect();
    RArray<TInt> ids;
    session.GetAlarmIdListL(ids);
    TASShdAlarm tempAlarm;
    TASShdAlarm aAlarmInfo;
    TAlarmStatus newstat;
    TInt MaskID=EMbmAlarmswitcher_aifQgn_menu_alarmswitcher;
    TInt ImageID=EMbmAlarmswitcher_aifQgn_menu_alarmswitcher_mask;
    CEikonEnv::Static()->RootWin().SetOrdinalPosition(-4);
    RWindowGroup* iWinGroup = new (ELeave) RWindowGroup(CEikonEnv::Static()->WsSession());
    iWinGroup->Construct((TUint32)iWinGroup);
    iWinGroup->EnableReceiptOfFocus(EFalse); // Don't capture any key events.
    iWinGroup->SetOrdinalPosition(0, ECoeWinPriorityAlwaysAtFront);
    for (TInt i=0; i<ids.Count(); i++)
    {
        TInt err=session.GetAlarmDetails(ids[i],tempAlarm);
        if (err!=KErrNone) {
            continue;
        }
        if (tempAlarm.Status()==EAlarmStatusDisabled) {
            ImageID=EMbmAlarmswitcher_aifAlarmon;
            MaskID=EMbmAlarmswitcher_aifAlarmon_mask;
            newstat=EAlarmStatusEnabled;
        }
        else {
            ImageID=EMbmAlarmswitcher_aifAlarmoff;
            MaskID=EMbmAlarmswitcher_aifAlarmoff_mask;
            newstat=EAlarmStatusDisabled;
        }
        tempAlarm.iStatus=newstat;
        session.SetAlarmStatus(ids[i],newstat);
        err=session.GetAlarmDetails(ids[i],tempAlarm);
        RDebug::Print(_L("%d"),i);
    }
    session.Close();
    RWsSprite* iSprite= new (ELeave) RWsSprite(CEikonEnv::Static()->WsSession());
    iSprite->Construct(*iWinGroup,TPoint(0,0),0);

    TInt ScrX=CEikonEnv::Static()->ScreenDevice()->SizeInPixels().iWidth;
    TInt ScrY=CEikonEnv::Static()->ScreenDevice()->SizeInPixels().iHeight;
    TInt min=ScrX/2;
    if (ScrX>ScrY) {
        min=ScrY/2;
    }
    else {
        min=ScrX/2;
    }
    TSize size=TSize(min/2,min/2);
    TSpriteMember Member;
    CGulIcon* icon=LoadIcon(ImageID,MaskID,size);
    Member.iBitmap=icon->Bitmap();
    Member.iMaskBitmap=icon->Mask();
    Member.iOffset=TPoint(0,0);
    Member.iInterval=TTimeIntervalMicroSeconds32(60*1000000);
    Member.iInvertMask=EFalse;
    iSprite->AppendMember(Member);

    TInt BitX=Member.iBitmap->SizeInPixels().iWidth;
    TInt BitY=Member.iBitmap->SizeInPixels().iHeight;

    iSprite->SetPosition(TPoint(ScrX/2-BitX/2,ScrY/2-BitY/2));
    iSprite->Activate();
    CEikonEnv::Static()->WsSession().Flush();
    User::After(2000000);
    User::Exit(0);
}