⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendarapiexampleengine.cpp

📁 该源码实现个人日程管理以及备忘功能
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CCalendarAPIexampleEngine from CalendarAPIexampleEngine.h
*  Part of  : Thread
*  Created  : 02/22/2005 by Forum Nokia
*  Version  : 1.0
*  Copyright: Nokia Corporation
* ============================================================================
*/

// INCLUDE FILES
#include <agclient.h>	//RAgendaServ
#include <agmmodel.h>	//CAgnModel
#include <txtfmlyr.h>	//CParaFormatLayer
#include <agmalarm.h>	//CAgnAlarm
#include "CalendarAPIexampleEngine.h"
#include "CalendarAPIexample.hrh"


// CONSTANTS
_LIT(KCalendarFile, "c:\\system\\data\\calendar");
_LIT(KDefaultTodoListName, "TODO");

#define KDefaultEventDisplayTime TTimeIntervalMinutes(660)
#define KDefaultAnnivDisplayTime TTimeIntervalMinutes(900)
#define KDefaultDayNoteDisplayTime TTimeIntervalMinutes(1000)


// ================= MEMBER FUNCTIONS =======================

// constructor
CCalendarAPIexampleEngine::CCalendarAPIexampleEngine()
	{
	}
	
// destructor
CCalendarAPIexampleEngine::~CCalendarAPIexampleEngine()
	{
	if (iAgnServer)
		{
		iAgnServer->Close();
		delete iAgnServer;
		}
	}
	
// Two-phased constructor.
CCalendarAPIexampleEngine* CCalendarAPIexampleEngine::NewL()
	{
	CCalendarAPIexampleEngine* self = new (ELeave) CCalendarAPIexampleEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}
	
// Symbian OS default constructor can leave.	
void CCalendarAPIexampleEngine::ConstructL()
	{
	iAgnServer = RAgendaServ::NewL();			// allocate and construct server
	User::LeaveIfError(iAgnServer->Connect());	// connect to the agenda server
	
	// Check that calendar exists, and if not, create it.
	// Calendar does not exist until it is created by calendar app - or by us.
	CAgnEntryModel* model = CAgnEntryModel::NewL(NULL);
	CleanupStack::PushL(model);
	model->SetServer(iAgnServer);
	TRAPD(err, model->OpenL(KCalendarFile));
	CleanupStack::PopAndDestroy(model);

	if(err == KErrNotFound)
		{
		CAgnEntryModel* model = CAgnEntryModel::NewL(NULL);
		CleanupStack::PushL(model);
		// Do not ->SetServer()!

		CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL();
		CleanupStack::PushL(paraFormatLayer);
		CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL();
		CleanupStack::PushL(charFormatLayer);
		RFs fs;
		User::LeaveIfError(fs.Connect());
		CleanupClosePushL(fs);

		TFileName fileName(KCalendarFile);
		model->CreateL( fs, fileName, KDefaultTodoListName, paraFormatLayer, charFormatLayer );
		CleanupStack::PopAndDestroy(4); // charFormatLayer, paraFormatLayer, fs, model

		// Reconnect to agenda server
		iAgnServer->Close();
		delete iAgnServer;
		iAgnServer = NULL;
		iAgnServer = RAgendaServ::NewL();
		User::LeaveIfError(iAgnServer->Connect());
		}	
	}


// ----------------------------------------------------
// CCalendarAPIexampleEngine::AddAnniversaryL()
// adds an anniversary to the agenda file
// ----------------------------------------------------
//	
void CCalendarAPIexampleEngine::AddAnniversaryL(CAgnAnniv& aAnniv) const
												
	{
	CAgnModel* model = CAgnModel::NewL(NULL);		// allocate and construct model
	CleanupStack::PushL(model);
	model->SetServer(iAgnServer);					// set server pointer for model
	model->OpenL(	KCalendarFile,
					KDefaultEventDisplayTime,
					KDefaultAnnivDisplayTime,
					KDefaultDayNoteDisplayTime);	// open file using server
					
	iAgnServer->WaitUntilLoaded();
	
	CAgnAlarm* alarm = CAgnAlarm::NewL(model);
	CleanupStack::PushL(alarm);
	model->RegisterAlarm(alarm);
	
	model->AddEntryL(&aAnniv);
	
	// update the alarm server, because the anniversary might have an alarm.
	alarm->FindAndQueueNextAlarmL();
	
	// this allows the alarm to be serviced after the session is closed
	alarm->OrphanAlarm();
	
	CleanupStack::PopAndDestroy(alarm);
	CleanupStack::PopAndDestroy(model);
	}

// ----------------------------------------------------
// CCalendarAPIexampleEngine::UpdateAnniversaryL()
// updates an anniversary
// ----------------------------------------------------
//		
void CCalendarAPIexampleEngine::UpdateAnniversaryL(	CAgnAnniv* aAnniv, 
													CAgnAnniv* aNewAnniv,
													const TAgnWhichInstances& aWhichInstances) const
	{
	CAgnModel* model = CAgnModel::NewL(NULL);		// allocate and construct model
	CleanupStack::PushL(model);
	model->SetServer(iAgnServer);					// set server pointer for model
	model->OpenL(	KCalendarFile,
					KDefaultEventDisplayTime,
					KDefaultAnnivDisplayTime,
					KDefaultDayNoteDisplayTime);	// open file using server

	iAgnServer->WaitUntilLoaded();
	
	CAgnAlarm* alarm = CAgnAlarm::NewL(model);
	CleanupStack::PushL(alarm);
	model->RegisterAlarm(alarm);	

	// if the date of the anniversary has changed, it has to be removed and then added again
	if (aNewAnniv)
		{
		model->DeleteInstanceL(aAnniv);
		delete aAnniv;
		model->AddEntryL(aNewAnniv);		
		aAnniv = aNewAnniv;
		}
	// the date of the anniversary hasn't changed, we only need to update the anniversary.
	else
		{
		model->UpdateInstanceL(aAnniv, aWhichInstances);
		delete aAnniv;		
		}
		
	// update the alarm server, because the alarm status of the anniversary might have changed.
	alarm->FindAndQueueNextAlarmL();
	
	// this allows the alarm to be serviced after the session is closed
	alarm->OrphanAlarm();	
	
	CleanupStack::PopAndDestroy(alarm);
	CleanupStack::PopAndDestroy(model);
	}

// ----------------------------------------------------
// CCalendarAPIexampleEngine::DeleteAnniversaryL()
// deletes an anniversary from the agenda file
// ----------------------------------------------------
//	
void CCalendarAPIexampleEngine::DeleteAnniversaryL(	CAgnAnniv* aAnniv, 
													const TAgnWhichInstances& aWhichInstances) const
													
	{
	CAgnModel* model = CAgnModel::NewL(NULL);		// allocate and construct model
	CleanupStack::PushL(model);
	model->SetServer(iAgnServer);					// set server pointer for model
	model->OpenL(	KCalendarFile,
					KDefaultEventDisplayTime,
					KDefaultAnnivDisplayTime,
					KDefaultDayNoteDisplayTime);	// open file using server

	iAgnServer->WaitUntilLoaded();
	
	CAgnAlarm* alarm = CAgnAlarm::NewL(model);
	CleanupStack::PushL(alarm);
	model->RegisterAlarm(alarm);	

	model->DeleteInstanceL(aAnniv, aWhichInstances);
	delete aAnniv;

	// update the alarm server. If the anniversary had an alarm, it will still be active
	// if this is not called.
	alarm->FindAndQueueNextAlarmL();
	
	CleanupStack::PopAndDestroy(alarm);
	CleanupStack::PopAndDestroy(model);
	}

// ----------------------------------------------------
// CCalendarAPIexampleEngine::GetAnniversariesL()
// finds anniversaries within given time interval.
// ----------------------------------------------------
//		
void CCalendarAPIexampleEngine::GetAnniversariesL(	const TDateTime& aFrom, 
													const TDateTime& aTo, 
													RPointerArray<CAgnAnniv>& aAnniversaries,
													MAgnProgressCallBack* aProgressCallBack)													
	{
	CAgnModel* model = CAgnModel::NewL(this);		// allocate and construct model
	CleanupStack::PushL(model);
	model->SetServer(iAgnServer);					// set server pointer for model
	model->SetMode(CAgnEntryModel::EClient); 
	// if callback provided, open asynchronously
	if (aProgressCallBack)
		{
		model->OpenL(	KCalendarFile,
						KDefaultEventDisplayTime,
						KDefaultAnnivDisplayTime,
						KDefaultDayNoteDisplayTime,
						aProgressCallBack);			// open file using server
		iAgnServer->WaitUntilLoaded();
		iWait.Start();		
		}
	// callback not provided, open synchronously
	else
		{
		model->OpenL(	KCalendarFile,
						KDefaultEventDisplayTime,
						KDefaultAnnivDisplayTime,
						KDefaultDayNoteDisplayTime);// open file using server
		iAgnServer->WaitUntilLoaded();
		}
	
	// search only anniversaries	
	TAgnDayFilter filter(model);
	filter.SetIncludeTodos(EFalse);
	filter.SetIncludeEvents(EFalse);
	filter.SetIncludeTimedAppts(EFalse);
	filter.SetIncludeUnTimedAppts(EFalse);

	TTime nextDate(aFrom);
	// decrease 'from' date by one day, so that 'from' date will be included in search.
	nextDate -= TTimeIntervalDays(1);
	TTime toDate(aTo);
	
	TTime today;
	today.HomeTime();
	
	nextDate = model->NextDayWithInstance(today, filter, nextDate);

	//while anniversaries are found and they are inside search range.
	while (nextDate.Int64().Low() != 0 && nextDate <= toDate)
		{
		CAgnDayList<TAgnInstanceId>* annivsList = CAgnDayList<TAgnInstanceId>::NewL(nextDate);
		CleanupStack::PushL(annivsList);
		model->PopulateDayInstanceListL(annivsList, filter, today);
		for (TInt i = 0; i < annivsList->Count(); i++)
			{
			TAgnInstanceId instanceId = (*annivsList)[i];
			CAgnEntry* entry = model->FetchInstanceL(instanceId);
			CleanupStack::PushL(entry);
			CAgnAnniv* anniv = entry->CastToAnniv();
			User::LeaveIfError(aAnniversaries.Append(anniv));
			CleanupStack::Pop(entry);
			}
			
		CleanupStack::PopAndDestroy(annivsList);
		
		nextDate = model->NextDayWithInstance(today, filter, nextDate);
		
		}
				
	CleanupStack::PopAndDestroy(model);
	}
	
// ----------------------------------------------------
// CCalendarAPIexampleEngine::StateCallBack()
// From MAgnModelStateCallBack. Called with a value of 
// EBlocked when the model is about to enter a blocked 
// state. This is caused by starting a prolonged 
// activity, e.g. opening, merging or tidying, and with
// EOk when it is about to leave the blocked state.
// ----------------------------------------------------
//		
void CCalendarAPIexampleEngine::StateCallBack(CAgnEntryModel::TState aState)
	{
	switch (aState)
		{
		case CAgnModel::ENoFile: 
			{
			iWait.AsyncStop();
			break;
			}
		case CAgnModel::EBlocked: 
			{
			break;
			}
		case CAgnModel::EOk: 
			{
			iWait.AsyncStop();
			break;
			}
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -