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

📄 calendarapiexampledocument.cpp

📁 该源码实现个人日程管理以及备忘功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ============================================================================
*  Name     : CCalendarAPIexampleDocument from CalendarAPIexampleDocument.h
*  Part of  : CalendarAPIexample
*  Created  : 02/22/2005 by Forum Nokia
*  Version  : 1.0
*  Copyright: Nokia Corporation
* ============================================================================
*/

// INCLUDE FILES
#include "CalendarAPIexampleDocument.h"
#include "CalendarAPIexampleAppUi.h"
#include "CalendarAPIexampleEngine.h"
#include "CalendarAPIexample.pan"
#include <agmrepli.h>

// CONSTANTS
const TInt KMaxSync = 2;
const TInt KDefaultAlarmHour = 8;
const TInt KUndefinedModifyIndex = -1;


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

// constructor
CCalendarAPIexampleDocument::CCalendarAPIexampleDocument(CEikApplication& aApp)
: CAknDocument(aApp)    
    {
    }

// destructor
CCalendarAPIexampleDocument::~CCalendarAPIexampleDocument()
    {
    delete iEngine;
	iEntries.ResetAndDestroy();
	delete iEntry;
    }

// Symbian OS default constructor can leave.
void CCalendarAPIexampleDocument::ConstructL()
    {
    iModifyIndex = KUndefinedModifyIndex;
    iEngine = CCalendarAPIexampleEngine::NewL();    
    }

// Two-phased constructor.
CCalendarAPIexampleDocument* CCalendarAPIexampleDocument::NewL(
        CEikApplication& aApp)     // CCalendarAPIexampleApp reference
    {
    CCalendarAPIexampleDocument* self = new (ELeave) CCalendarAPIexampleDocument( aApp );
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop();

    return self;
    }
    
// ----------------------------------------------------
// CCalendarAPIexampleDocument::CreateAppUiL()
// constructs CCalendarAPIexampleAppUi
// ----------------------------------------------------
//
CEikAppUi* CCalendarAPIexampleDocument::CreateAppUiL()
    {
    return new (ELeave) CCalendarAPIexampleAppUi;
    }

// ----------------------------------------------------
// CCalendarAPIexampleDocument::EntryCount()
// returns the number of found anniversaries.
// ----------------------------------------------------
//	
TInt CCalendarAPIexampleDocument::EntryCount() const
	{
	return iEntries.Count();
	}

// ----------------------------------------------------
// CCalendarAPIexampleDocument::Entry()
// returns a reference to an entry for reading the values 
// of the entry. Panics with EOutOfEntriesArray if parameter 
// aIndex is negative or greater than the number of entries
// ----------------------------------------------------
//		
CEntry& CCalendarAPIexampleDocument::Entry(const TInt& aIndex)
	{
	if (0 > aIndex || iEntries.Count() <= aIndex)
		{
		Panic(EOutOfEntriesArray);
		}
		
	return *iEntries[aIndex];
	}

// ----------------------------------------------------
// CCalendarAPIexampleDocument::DoSearchL()
// Searches all anniversaries in given time interval.
// ----------------------------------------------------
//		
void CCalendarAPIexampleDocument::DoSearchL(const TSearchType& aType,
											MAgnProgressCallBack* aProgressCallBack)
	{
	iEntries.ResetAndDestroy();
	iCurrentSearchType = aType;
	TTime now;
	now.HomeTime();
	TDateTime from = now.DateTime();
	User::LeaveIfError(from.SetHour(0));
	User::LeaveIfError(from.SetMinute(0));
	User::LeaveIfError(from.SetSecond(0));
	User::LeaveIfError(from.SetMicroSecond(0));
	
	//set the time interval
	switch (aType)
		{
		case EWeek:
			now += TTimeIntervalDays(7);
			break;
		case EMonth:
			now += TTimeIntervalMonths(1);
			break;
		case ESixMonths:
			now += TTimeIntervalMonths(6);
			break;
		case EYear:
			now += TTimeIntervalYears(1);
			break;
		}

	TDateTime to = now.DateTime();
	
	RPointerArray<CAgnAnniv> annivs;
	iEngine->GetAnniversariesL(from, to, annivs, aProgressCallBack);
	
	//create new CEntry for each CAgnAnniv.
	for (TInt i = 0; i < annivs.Count(); i++)
		{
		CEntry* entry = CEntry::NewLC(annivs[i]);
		User::LeaveIfError(iEntries.Append(entry));
		CleanupStack::Pop(entry);
		}

	annivs.Reset();
	}

// ----------------------------------------------------
// CCalendarAPIexampleDocument::ResetModifyIndex()
// Resets the modify index.
// ----------------------------------------------------
//	
void CCalendarAPIexampleDocument::ResetModifyIndex()
	{
	iModifyIndex = KUndefinedModifyIndex;
	}

// ----------------------------------------------------
// CCalendarAPIexampleDocument::ModifyIndex()
// Returns the modify index.
// ----------------------------------------------------
//	
TInt CCalendarAPIexampleDocument::ModifyIndex() const
	{
	return iModifyIndex;
	}
	
// ----------------------------------------------------
// CCalendarAPIexampleDocument::SetModifyIndex()
// Sets the modify index. Panics with KInvalidModifyIndex
// if parameter aIndex is negative or greater than the
// number of entries.
// ----------------------------------------------------
//		
void CCalendarAPIexampleDocument::SetModifyIndex(const TInt& aIndex)
	{
	if (aIndex < 0 || aIndex >= iEntries.Count())
		{
		Panic(KInvalidModifyIndex);
		}
		
	iModifyIndex = aIndex;
	}

// ----------------------------------------------------
// CCalendarAPIexampleDocument::EntryForModificationL()
// Returns an entry for modification. Creates a new 
// entry if iModifyIndex hasn't been set. Panics with
// KInvalidModifyIndex if iModifyIndex is greater than
// the number of entries.
// ----------------------------------------------------
//		
CEntry& CCalendarAPIexampleDocument::EntryForModificationL(TBool& aModify)
	{
	delete iEntry;
	iEntry = NULL;

	//No selected entry, create a new one for adding an entry.
	if (iModifyIndex < 0)
		{
		aModify = EFalse;
		iEntry = CEntry::NewL(NULL);
		return *iEntry;
		}

	if (iModifyIndex >= iEntries.Count())
		{
		Panic(KInvalidModifyIndex);
		}
		
	aModify = ETrue;
	
	//return selected entry for modification.
	return *iEntries[iModifyIndex];
	}

// ----------------------------------------------------
// CCalendarAPIexampleDocument::SaveL()
// Adds or updates the current entry to the agenda file.
// ----------------------------------------------------
//		
void CCalendarAPIexampleDocument::SaveL()
	{
	//add a new anniversary
	if (iEntry)
		{
		if (iEntry->Modified())
			{
			iEntry->SaveValuesL();
			CAgnAnniv* anniv = iEntry->Anniv();
			iEngine->AddAnniversaryL(*anniv);
			delete anniv;
			delete iEntry;
			iEntry = NULL;
			}
		}
	//modify existing anniversary
	else
		{
		if (0 > iModifyIndex || iEntries.Count() <= iModifyIndex)
			{
			Panic(KInvalidModifyIndex);
			}
			
		CEntry* entry = iEntries[iModifyIndex];

		if (entry->Modified())
			{
			//if the date of the entry has changed, the entry must first be removed and then added again.
			if (entry->DateHasChanged())
				{
				CAgnAnniv* updateAnniv = entry->Anniv();
				CAgnAnniv* newAnniv = entry->NewAnnivLC();
				
				iEngine->UpdateAnniversaryL(updateAnniv, newAnniv);

				CleanupStack::PopAndDestroy(newAnniv);
				}
			//the date of the entry hasn't changed, the entry is just updated.
			else
				{
				entry->SaveValuesL();
				CAgnAnniv* updateAnniv = entry->Anniv();
				iEngine->UpdateAnniversaryL(updateAnniv);
				}
			
			DoSearchL(iCurrentSearchType);
			}		
		}
	}
	
// ----------------------------------------------------
// CCalendarAPIexampleDocument::DeleteEntryL()
// Deletes an anniversary from the agenda file. Panics
// with KInvalidEntryIndex if parameter aIndex is negative
// or greater than the value of entries.
// ----------------------------------------------------
//			
void CCalendarAPIexampleDocument::DeleteEntryL(const TInt& aIndex)
	{
	if (aIndex < 0 || aIndex >= iEntries.Count())
		{
		Panic(KInvalidEntryIndex);
		}
		
	CEntry* entry = iEntries[aIndex];
	iEngine->DeleteAnniversaryL(entry->Anniv());
	iEntries.Remove(aIndex);
	delete entry;
	}
	
	


	
// ----------------------------------------------------
// 		CEntry - a wrapper class for CAgnAnniv
// ----------------------------------------------------
	
// ================= MEMBER FUNCTIONS =======================	


// Two-phased constructor.	
CEntry* CEntry::NewL(CAgnAnniv* aAnniv)
	{
	CEntry* self = CEntry::NewLC(aAnniv);
	CleanupStack::Pop(self);
	return self;
	}
	
// Two-phased constructor.	
CEntry* CEntry::NewLC(CAgnAnniv* aAnniv)
	{
	CEntry* self = new (ELeave) CEntry;
	CleanupStack::PushL(self);
	self->ConstructL(aAnniv);
	return self;
	}

// ----------------------------------------------------
// Standard Symbian OS 2nd phase constructor
// Will initialize entry with values of parameter aAnniv,
// if parameter aAnniv is provided (not NULL).
// ----------------------------------------------------
//		
void CEntry::ConstructL(CAgnAnniv* aAnniv)
	{
	// initialize today's date for anniversary date and alarm.
	TTime now;
	now.HomeTime();
	iDate = now.DateTime();
	iAlarmTime = now.DateTime();
	User::LeaveIfError(iAlarmTime.SetHour(KDefaultAlarmHour));
	User::LeaveIfError(iAlarmTime.SetMinute(0));
	User::LeaveIfError(iAlarmTime.SetSecond(0));
	iAlarmDays = TTimeIntervalDays(0);
	iAlarmMinutes = TTimeIntervalMinutes(KDefaultAlarmHour*60);
	

	if (aAnniv == NULL)
		{
		return;
		}
		
	iAnniv = aAnniv;

	// read the name for the anniversary.
	TBuf<KMaxNameLength> name;
	aAnniv->RichTextL()->Extract(name,0);
	iName = name;

	// read the date for the anniversary.
	iDate = aAnniv->InstanceStartDate().DateTime();

	// read the age for the anniversary
	//iAge = iDate.Year() - aAnniv->BaseYear().Int(); Recommanded YC
	iAge = iDate.Year() - aAnniv->BaseYear().Int();

	iAlarm = aAnniv->HasAlarm();
	// if the anniversary has an alarm, read it and extract the data to the 
	// correnct variables of the entry.
	if (iAlarm)
		{
		iAlarmTime = aAnniv->AlarmInstanceDateTime().DateTime();
		GetDaysAndMinutesL(iDate, iAlarmTime, iAlarmDays, iAlarmMinutes);
		}

	// read how the anniversary is to be synchronized.
	TAgnReplicationData annivSynchronizationData = aAnniv->ReplicationData();
	if (annivSynchronizationData.Status() == TAgnReplicationData::EOpen)
		{
		SetSynchronizationMethod(KSyncPublic);
		}
	else if (annivSynchronizationData.Status() == TAgnReplicationData::EPrivate)
		{
		SetSynchronizationMethod(KSyncPrivate);
		}
	else
		{
		SetSynchronizationMethod(KSyncNo);
		}

	iModified = EFalse;
	}

// constructor	
CEntry::CEntry()
	{
	}
// destructor	
CEntry::~CEntry()
	{

⌨️ 快捷键说明

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