pimappservices/calendar/server/inc/agsentrymodel.h
changeset 0 f979ecb2b13e
child 12 38571fd2a704
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifndef __AGSENTRYMODEL_H__
       
    17 #define __AGSENTRYMODEL_H__
       
    18 #include <f32file.h>
       
    19 #include "agsver.h"
       
    20 #include <calattachmentmanager.h>
       
    21 #include <calchangecallback.h>
       
    22 #include <calentry.h>
       
    23 #include "calinstanceviewimpl.h"
       
    24 #include <f32file.h>
       
    25 
       
    26 class CAgnAlarm;
       
    27 class CAgnAttachment;
       
    28 class CAgnAttachmentFile;
       
    29 class CAgnAttachmentIndex;
       
    30 class CAgnCalendarConverter;
       
    31 class CAgnCategoryIndex;
       
    32 class CAgnContent;
       
    33 class CAgnEntry;
       
    34 class CAgnEntryManager;
       
    35 class CAgnModelStreamIdSet;
       
    36 class CAgnRptDef;
       
    37 class CAgnServerSession;
       
    38 class CAgnServFile;
       
    39 class CAgnSimpleEntryTable;
       
    40 class CAgnInstanceInfo;
       
    41 class CCalAsyncDelete;
       
    42 class CStreamStore;
       
    43 class RFile;
       
    44 class TAgnChangeFilter;
       
    45 class TAgnEntryIter;
       
    46 class TAgnFilter;
       
    47 class TAgnInstanceExtractor;
       
    48 class TAgnSortInstance;
       
    49 class TFindInstanceParams;
       
    50 class CAgnTzRuleIndex;
       
    51 class CAgsInstanceIteratorMgr;
       
    52 
       
    53 // function pointer used in text matching 
       
    54 typedef TInt (*MatchTextFnPtr)(const TDesC& aTextField, const TDesC& aSearchText);
       
    55 
       
    56 /** This class represents details of a database change to be notified to clients. 
       
    57 Most of this information is used for filtering.
       
    58 @internalComponent
       
    59 */
       
    60 struct TAgnChange
       
    61 	{
       
    62 	// The start and end time of the entry affected
       
    63 	TTime							iStartTimeOfEntryUtc;
       
    64 	TTime							iEndTimeOfEntryUtc;
       
    65 	
       
    66 	// The repeat rule of the entry affected (NULL if there isn't one)
       
    67 	const CAgnRptDef*				iRepeatRule;
       
    68 
       
    69 	// If the operation is an update, the start and end time of the original entry
       
    70 	TTime							iOriginalStartTimeUtc;
       
    71 	TTime							iOriginalEndTimeUtc;
       
    72 	
       
    73 	// If the operation is an update, the repeat rule of the original entry (NULL if there isn't one)
       
    74 	const CAgnRptDef*				iOriginalRepeatRule;
       
    75 
       
    76 	// The type of operation
       
    77 	MCalChangeCallBack2::TChangeType	iOperationType;
       
    78 	
       
    79 	// The type of entry
       
    80 	CCalEntry::TType				iEntryType;
       
    81 
       
    82 	// The id of the entry
       
    83 	TCalLocalUid					iEntryId;
       
    84 
       
    85 	// The session which made the operation (not owned)
       
    86 	CAgnServerSession*				iSession;
       
    87 	TInt64							iFileId;
       
    88 	};
       
    89 
       
    90 
       
    91 /** The server-side model to control access to entries.
       
    92 
       
    93 This class contains the main functionality for adding, updating, deleting and fetching entries, and 
       
    94 searching for instances.
       
    95 It is responsible for change notification, queuing alarms, building the entry indexes, opening calendar files, 
       
    96 and carrying out roll-back if an error occurs.
       
    97 
       
    98 CAgnEntryModel references other server-side classes which are used for various functions:
       
    99 - CAgnEntryManager takes responsibility for all file-reading and file-writing operations, e.g. adding an entry 
       
   100 to the calendar file.
       
   101 - CAgnServFile is used to control higher level file access operations, e.g. opening a file.
       
   102 - CAgnSimpleEntryTable contains the entry indexes. CAgnCategoryIndex and CAgnAttachmentIndex contain
       
   103 other indexes for controlling category and attachment data.
       
   104 - TAgnInstanceExtractor is used for all instance searching operations, including finding alarmed instances when 
       
   105 queuing alarms.
       
   106 
       
   107 @see CAgnEntryModel 
       
   108 @internalComponent
       
   109 @released
       
   110 */
       
   111 class CAgnEntryModel : public CBase
       
   112 	{
       
   113 public:
       
   114 	friend class CAgnCalendarConverter; // to allow access to UpdateIndexL
       
   115 	
       
   116 	enum TUpdateIndex
       
   117 		{
       
   118 		EAdd,
       
   119 		EDelete,
       
   120 		EUpdate,
       
   121 		EBuildIndex
       
   122 		};
       
   123 	
       
   124 // construction / destruction
       
   125 	static CAgnEntryModel* NewL(CAgnServFile* aAgnServerFile);
       
   126 	~CAgnEntryModel();
       
   127 
       
   128 // file operations
       
   129 	void OpenL(CStreamStore& aStore, const TStreamId& aModelStreamId);
       
   130 	TStreamId CreateL(CStreamStore& aStore);
       
   131 	const TInt64& GetFileIdL();
       
   132 	const TDesC& FileName() const;
       
   133 	
       
   134 	void CommitL();
       
   135 	void Rollback();
       
   136 	void CommitAndNotifyDeletesL(TAgnChangeFilter& aChangeFilter);
       
   137 
       
   138 // build indexes
       
   139 	void ResetIndexes();
       
   140 	TInt DoIndexBuildStepL(); // return percentage complete
       
   141 	void BuildIndexCompleteL();
       
   142 	TBool AreIndexesBuilt() const;
       
   143 	TBool StreamsAreEmpty() const;
       
   144 
       
   145 // queueing alarms
       
   146 	void NextAlarmForServerL(const TTime& aNow,CArrayFixFlat<TAgnSortInstance>& aAlarmedIds);
       
   147 	void NextFewAlarmsForServerL(const TTime& aStartDateTime,const TTime& aEndDateTime,CArrayFixFlat<TAgnSortInstance>& aAlarmedIds,const TInt aMaxNumberOfAlarms);
       
   148 	void FindAndQueueNextFewAlarmsL();
       
   149 	void SetUpdateAlarmL(TBool aUpdateAlarm);
       
   150 #ifdef SYMBIAN_SYSTEM_STATE_MANAGEMENT
       
   151 	void DeleteAlarmsAndRequeueSessionAlarmL();
       
   152 #endif
       
   153 // SyncML requirements
       
   154 	void GetEntryUidsSinceDateL(const TTime& aTime, RArray<TCalLocalUid>& aUniqueIdList); // find entries modified since a certain time
       
   155 
       
   156 // access entry's attributes which are not stored with entries
       
   157 	HBufC* RestoreTextL(const TStreamId& aStream);
       
   158 	CAgnContent* RestoreAlarmActionL(const TStreamId& aStream);
       
   159 
       
   160 // entry operations
       
   161 	TAgnEntryId StoreL(CAgnEntry& aEntry, TAgnChangeFilter* aChangeFilter);
       
   162 	void UpdateEntryL(CAgnEntry& aNewEntry, TAgnChangeFilter* aChangeFilter, TBool aDeleteChildren);
       
   163     void DeleteEntryL(CAgnEntry& aEntry, TBool aCascadeDelete, TAgnChangeFilter* aChangeFilter);
       
   164     
       
   165 // fetching entries
       
   166 	const CAgnSimpleEntry* GetSimpleEntryFromIndexes(const TAgnEntryId& aId);
       
   167 	const CAgnSimpleEntry* GetSimpleEntryFromIndexes(TCalLocalUid aId);
       
   168 	CAgnEntry* FetchEntryL(const TAgnEntryId& aId) const;	
       
   169 	CAgnEntry* FetchEntryL(TCalLocalUid aId) const;
       
   170 	CAgnEntry* FetchEntryL(const TDesC8& aGuid) const;
       
   171 	void FetchEntriesL(const TDesC8& aGuid, RPointerArray<CAgnEntry>& aList) const;
       
   172 	
       
   173 // instance searching
       
   174 	void FindInstancesL(CArrayFix<TAgnSortInstance>& aInstances, const TFindInstanceParams& aParameters);
       
   175 	void NextPossibleInstancesL(CArrayFix<TAgnSortInstance>& aInstances, const TFindInstanceParams& aSearchParams) const;
       
   176 	void PreviousPossibleInstancesL(CArrayFix<TAgnSortInstance>& aInstances, const TFindInstanceParams& aSearchParams) const;
       
   177 	void LogInstanceSearchL(const TFindInstanceParams& aParameters, const CArrayFix<TAgnSortInstance>& aInstances) const;
       
   178 
       
   179 // used by extractors when searching
       
   180 	TBool MatchFullEntryL(const TAgnEntryId& aEntryId, const TFindInstanceParams& aSearchParams);
       
   181 	TBool MatchSearchTextL(CAgnEntry& aEntry, const TDesC& aSearchText, const TAgnFilter& aFilter);
       
   182 	static TBool CheckStartAndEndDateOverlap(const TAgnSortInstance& aInstance, const TFindInstanceParams& aSearchParams);
       
   183 
       
   184 // notifications
       
   185 	void NotifyChangeL(const CAgnServerSession& aSession, CAgnEntry* aEntry, MCalChangeCallBack2::TChangeType aChangeType, CAgnInstanceInfo* aOriginalEntry);
       
   186 	void NotifyPublishAndSubscribeL(TAgnChangeFilter& aChangeFilter);
       
   187 
       
   188 // Attachments
       
   189 	TInt TransferAttachmentFileToServerL(RFile& aFile, TCalLocalUid aLocalUid, TInt aAttachmentIndex);
       
   190 	void OpenAttachmentFileL(RFile& aFile, TInt aAttachmentIndex) const;
       
   191 	HBufC* GenerateFilenameLC(const TDesC& aDrive, const TDesC& aFileName);
       
   192 	HBufC* GenerateRandomFilenameLC(const TDesC& aDrive);
       
   193 
       
   194 	void UpdateAttachmentDetailsL(TCalLocalUid aLocalUid, TInt aAttachmentIndex, const TDesC& aFileName, TInt aAttachmentSize);
       
   195 	void UpdateAttachmentDetailsOnCopyL(TCalLocalUid aLocalUid, TInt aAttachIndex, const TDesC& aFileName);
       
   196 	void CreateNewFileL(RFile& aFileHandle, const TDesC& aFileName);
       
   197 	const RArray<TCalLocalUid>* GetEntriesWithAttachment(TUint32 aAttachmentUid) const;
       
   198 	void GetSortedAttachmentsL(RArray<TUint32>& aAttachmentIds, CCalAttachmentManager::TSortOrder aSortOrder);
       
   199 	CAgnAttachment* FetchAttachmentByIdL(TUint32 aAttachUid);
       
   200 	TInt MoveFileToServerL(TCalLocalUid aLocalUid, TInt aAttachmentIndex);
       
   201 
       
   202 // Accessor functions
       
   203 	CAgnCategoryIndex& CategoryIndex() const;
       
   204 
       
   205 // Miscellaneous
       
   206 	const TDesC8& GetEntryGuidL(CAgnEntry& aEntry) const;
       
   207 	void SetBufferedDeleting(TBool aSetting);	
       
   208 	void FlushL();
       
   209 	void RestoreCategoriesL();
       
   210 	CCalAsyncDelete* CreateAsyncDeleteL(TAgnChangeFilter& aChangeFilter);
       
   211 	TAgnEntryIter* CreateEntryIterL() const;
       
   212 	TTime TzRulesLastModifiedDateL();
       
   213 	void CheckTzDbModificationL();
       
   214 	void HandleTzRulesChangeL(const TTime& aTime);
       
   215 
       
   216 // Calendar file conversion operations
       
   217 	void LoadNewStreamStoreL(CStreamStore& aStore, const TStreamId& aModelStreamId, CAgnEntryManager& aEntryManager, CAgnTzRuleIndex& aTzRuleIndex);
       
   218 	
       
   219 // Alarm accessor
       
   220    CAgnAlarm& Alarm();
       
   221 
       
   222 //methods for cached index file
       
   223 public:
       
   224 	TBool IsIndexFileDirty() const;
       
   225 	void  MarkIndexFileAsDirtyL();
       
   226 	TBool DoLoadIndexFile();
       
   227 	TBool LoadIndexFileL();
       
   228 	void  SaveIndexFileL();
       
   229 	TBool GenerateIndexFileName(TFileName& aFileName);
       
   230 	TCalCollectionId CollectionId() const;
       
   231 	const CAgnServFile& AgnServFile();
       
   232 
       
   233 
       
   234 private:
       
   235 	void ConstructL(CAgnServFile* aAgnServerFile);
       
   236 
       
   237 // file functions
       
   238 	void Reset();	
       
   239 	void DoOpenL(const TStreamId& aId);
       
   240 	void ExternalizeNextUidValuesL() const;
       
   241 	void ExternalizeNextUidValuesL(CStreamStore& aStore, const TStreamId& aStreamId) const;
       
   242 	void InternalizeNextUidValuesL();
       
   243 	void ExternalizeEntryManagerL() const;
       
   244 	void InternalizeEntryManagerL();
       
   245 	void ExternalizeFileIdL(CStreamStore& aStore, const TStreamId& aStreamId) const;
       
   246 
       
   247 // entry operations
       
   248 	TAgnEntryId AddEntryL(CAgnEntry& aEntry);
       
   249 	TAgnEntryId DoAddEntryL(CAgnEntry& aEntry);
       
   250    	void DoDeleteEntryL(CAgnEntry& aEntry);
       
   251 	void DoUpdateEntryL(CAgnEntry& aEntry, CAgnEntry* aOldEntry);
       
   252 	void DoUpdateAttachmentsL(CAgnEntry& aEntry, CAgnEntry& aOldEntry);
       
   253 
       
   254 	CAgnEntry* FetchEntryL(const TDesC8& aGuid, const TAgnCalendarTime& aRecurrenceId) const;
       
   255 	void MoveAttachmentToDriveL(CAgnAttachmentFile& aFileAttachment, CAgnAttachmentFile& aNewFileAttachment);
       
   256 	
       
   257 	void DoCommitL();
       
   258 	
       
   259 // parent-child functionality
       
   260 	TAgnEntryId AddChildEntryL(CAgnEntry& aChild, CAgnEntry& aParent);
       
   261 	TAgnEntryId AddRepeatingChildEntryUpdateParentRuleL(CAgnEntry& aParentEntry, CAgnEntry& aRepeatingChild);
       
   262 	TAgnEntryId AddNonRepeatingChildEntryUpdateParentExceptionsL(CAgnEntry& aParentEntry, CAgnEntry& aNonRepeatingChild);
       
   263 
       
   264 	TBool EntryHasNoChildrenAndNoValidInstancesL(CAgnEntry& aEntry) const;
       
   265 	TInt EntryHasRepeatingChildrenL(const CAgnEntry& aParentEntry);
       
   266 	void DeleteNonRepeatingChildrenOutsideRangeL(const CAgnEntry& aParentEntry, const TAgnCalendarTime& aRecIdUtc, CalCommon::TRecurrenceRange aRange);
       
   267  	void DeleteChildrenL(CAgnEntry& aParent);
       
   268 	void UpdateParentL(CAgnEntry& aChild);
       
   269     CAgnEntry* FindChildFromParentL(const CAgnEntry& aParent, const TAgnCalendarTime& aRecurrenceId) const;
       
   270 	
       
   271 // entry properties stored in separate streams
       
   272 	TStreamId StoreTextL(const TDesC& aText);
       
   273 	void UpdateTextL(const TDesC& aText, const TStreamId& aStream);
       
   274 	void DeleteTextStreamL(const TStreamId& aStream);
       
   275 	TStreamId StoreAlarmActionL(const CAgnContent& aAlarmAction);
       
   276 	void UpdateAlarmActionL(const CAgnContent& aAlarmAction, const TStreamId& aStream);
       
   277 	void DeleteAlarmActionStreamL(const TStreamId& aStream);
       
   278 
       
   279 	TStreamId WriteDescriptorToStreamL(const TDesC& aString);
       
   280 	void StoreExternalAttributesL(CAgnEntry& aEntry);
       
   281 	void DeleteExternalAttributesL(CAgnEntry& aEntry);
       
   282 	void UpdateExternalAttributesL(CAgnEntry& aEntry);
       
   283 
       
   284 // used when text searching
       
   285 	TBool MatchSearchTextL(MatchTextFnPtr aMatchTextFunction, CAgnEntry& aEntry, const TDesC& aSearchText, const TAgnFilter& aFilter);
       
   286 	static TInt MatchExactText(const TDesC& aTextField, const TDesC& aSearchText);
       
   287 	static TInt MatchFoldedText(const TDesC& aTextField, const TDesC& aSearchText);
       
   288 
       
   289 // accessor functions	
       
   290 	CStreamStore& StreamStore() const;
       
   291 	
       
   292 // alarm queuing
       
   293 	void CreateAlarmForServerL();	
       
   294 	void UpdateAlarmListL(CArrayFixFlat<TAgnSortInstance>& aAlarmedIds,CArrayFixSeg<TAgnSortInstance>& aDayInfoList, const TTime& aNow);
       
   295 	void AddToAlarmListL(CArrayFixFlat<TAgnSortInstance>& aAlarmedIds,CArrayFixSeg<TAgnSortInstance>& aDayInfoList, const TTime& aStartDateTime,
       
   296 					const TTime& aEndDateTime, const TInt aMaxNumberOfAlarms);
       
   297 	
       
   298 // notification
       
   299 	void NotifyingL(MCalChangeCallBack2::TChangeType aChangeType, CAgnEntry& aEntry, CAgnInstanceInfo* aOriginalEntry);
       
   300 	void NotifySessionsOfChangeL(const TAgnChange& aChange);
       
   301 
       
   302 // index updates
       
   303 	void UpdateIndexL(CAgnEntry& aEntry, CAgnEntry* aOldEntry, TUpdateIndex aUpdateIndex);
       
   304 	void AddEntryToIndexesL(CAgnEntry& aEntry);
       
   305 
       
   306 // rollback functions
       
   307 	void AppendRollbackArrayL(const CAgnEntry& aEntry, TBool aAdd);
       
   308 	void ResetRollback();
       
   309 	void RollbackIndexesL();
       
   310 // Miscellaneous
       
   311 
       
   312 	TInt TransferFileFromClientL(RFile& aAttachfileHandle, CAgnAttachmentFile& aAttachFile, CAgnEntry& aEntry, TBool aIsSameDrive);
       
   313 	TInt DoMoveFileToServerL(TCalLocalUid aLocalUid, TInt aAttachmentIndex);
       
   314 	void CopyAttachmentFileToDifferentPlaceL(CAgnEntry& aEntry);
       
   315 	TBool IsAttachmentFileFromSameSessionL(const TDesC& aAttachmentFile);	
       
   316 private:	  
       
   317 	TInt									iNumStreamsProcessed; 
       
   318 	CAgnModelStreamIdSet*					iModelStreamIdSet;
       
   319 	CAgnEntryManager* 						iEntryManager;
       
   320 	CAgnAlarm*								iAlarm;
       
   321 	TUint									iNextLocalUidValue;
       
   322 	TUint									iNextAttachmentUid;
       
   323 	TAgnInstanceExtractor*                  iExtractor;
       
   324 	CAgnSimpleEntryTable*					iSimpleEntryTable;
       
   325 	CAgnCategoryIndex*						iCategoryIndex;
       
   326 	CAgnAttachmentIndex*					iAttachmentIndex;
       
   327 	CAgnTzRuleIndex*						iTzRuleIndex;	
       
   328 	TInt64									iFileId;
       
   329 	CAgnServFile* 							iAgnServerFile;	//not owned
       
   330 	TBool									iUpdateAlarm;
       
   331 	TAgnChangeFilter*						iChangeFilter;//not owned
       
   332 	RArray<TAgnEntryId> 					iAddRollbackArray;
       
   333 	RPointerArray<CAgnEntry> 				iDeleteRollbackArray;
       
   334 	CAgnCalendarConverter*                  iCalConverter;
       
   335 	TInt									iOperationsCounter;
       
   336     RFile									iAttachmentFileHandle;
       
   337 	RFs										iFs;
       
   338 	TBool  									iIndexFileIsDirty;
       
   339 	TBool  									iIndexFileIsPresent;
       
   340 	CAgsInstanceIteratorMgr*				iInstanceIteratorMgr;
       
   341 	
       
   342 	};
       
   343 
       
   344 #endif
       
   345 
       
   346