phonebookengines/contactsmodel/cntplsql/inc/persistencelayerimpl.h
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /**
       
     2 * Copyright (c) 2005-2009 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 /**
       
    21  @file
       
    22  @internalComponent
       
    23  @released
       
    24 */
       
    25 
       
    26 #ifndef __PERSISTENCELAYERIMPL_H__
       
    27 #define __PERSISTENCELAYERIMPL_H__
       
    28 
       
    29 #include "persistencelayer.h" // For interface definitions.
       
    30 #include "pplcontactitemmanager.h"
       
    31 #include "pltables.h"
       
    32 #include <sqldb.h>
       
    33 class CContactItemManger;
       
    34 class CCntSqlStatement;
       
    35 
       
    36 #define KIgnorePhbkSync 0
       
    37 
       
    38 /**
       
    39 The CPplContactsFile class represents a single contacts database file and
       
    40 provides all common database file operations such as open, close, create and
       
    41 delete. 
       
    42 */
       
    43 class NONSHARED CPplContactsFile:  public CBase, public MLplContactsFile, public MLplTransactionManager
       
    44 	{
       
    45 public: // MLplContactsFile interface.
       
    46 
       
    47 	static CPplContactsFile* NewL(CLplContactProperties& aProps, MContactDbObserver* aObserver);
       
    48 	~CPplContactsFile();
       
    49 
       
    50 	IMPORT_C void CreateL(const TDesC& aFileName, TPlCreateMode aMode = EPlLeaveIfExist);
       
    51 	void OpenL(const TDesC& aFileName, TBool aNotify = EFalse);  
       
    52 	void Close(TBool aNotify = EFalse);
       
    53 	void DeleteL(const TDesC& aFileName);
       
    54 	CDesCArray* ListL(TDriveUnit* aDriveUnit = NULL);
       
    55 	inline TBool IsOpened() const;
       
    56 	virtual void CloseTablesL(TBool aNotify);
       
    57 	virtual void OpenTablesL(TBool aNotify = EFalse);	
       
    58 
       
    59 	inline MLplPersistenceBroker& PersistenceBroker();
       
    60 	inline MLplTransactionManager& TransactionManager();
       
    61 	inline MLplFieldMatcher& FieldMatcher();
       
    62 	
       
    63 	void DatabaseDrive(TDriveUnit& aDriveUnit);
       
    64 	TBool DatabaseExistsL(const TDesC& aFileName);
       
    65 	TInt FileSize() const;
       
    66 	
       
    67 	// Non-interface methods.
       
    68 	void RegisterDbObserver(MContactDbObserver& aDbObserver);
       
    69 	inline RSqlDatabase& NamedDatabase();
       
    70 	inline CLplContactProperties& ContactProperties();
       
    71 
       
    72 	void StartTransactionL();
       
    73 	void CommitCurrentTransactionL(TUint aSessionId);
       
    74 	void RollbackCurrentTransactionL(TUint aSessionId);
       
    75 	inline TBool IsTransactionActive() const;
       
    76 
       
    77 	void NotifyObserver() const;
       
    78 	
       
    79     void AddSqlDBObserverL( MLplSqlDatabaseObserver& aSqlDatabaseObserver );
       
    80     void RemoveSqlDBObserverL( MLplSqlDatabaseObserver& aSqlDatabaseObserver );
       
    81 
       
    82 private: // General methods used to implement all interfaces.
       
    83 	CPplContactsFile(CLplContactProperties& aProps, MContactDbObserver* aObserver);
       
    84 	void ConstructL();
       
    85 
       
    86 	void GenerateNotificationEventL(TBool aNotify = EFalse);
       
    87 
       
    88 	void GetPhysicalPathL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName);
       
    89 	void GetPhysicalFileNameL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName);
       
    90 
       
    91 	inline void LocalFsL();
       
    92 
       
    93 private: // Member variables used by several interfaces.
       
    94 	TBool iActive;  // Member variables used by MLplTransactionManager interface.
       
    95 
       
    96 	RFs iLocalFs;
       
    97 	
       
    98 	CPplContactItemManager* iItemManager;
       
    99   	MContactDbObserver* iDbObserver;  
       
   100   	CLplContactProperties& iContactProperties;
       
   101 
       
   102 	RSqlDatabase iDatabase;     // owned
       
   103 	TDriveUnit iDatabaseDrive;
       
   104 	TBool iFileIsOpen;
       
   105 	
       
   106 	RPplIccContactStore iIccContactStore; //owned
       
   107 	HBufC8* iConfigureStr;
       
   108 	
       
   109 	// The array of observers which are monitoring RSqlDatabase.
       
   110     RPointerArray<MLplSqlDatabaseObserver> iSqlDatabaseObservers;
       
   111 	};
       
   112 
       
   113 
       
   114 inline void CPplContactsFile::LocalFsL()
       
   115   	{
       
   116   	if(iLocalFs.Handle() == 0)
       
   117   		{
       
   118   		User::LeaveIfError(iLocalFs.Connect());
       
   119   		}
       
   120   	}
       
   121 
       
   122 
       
   123 inline RSqlDatabase& CPplContactsFile::NamedDatabase()
       
   124 	{
       
   125 	return iDatabase;
       
   126 	}
       
   127 
       
   128 
       
   129 inline CLplContactProperties& CPplContactsFile::ContactProperties()
       
   130 	{
       
   131 	return iContactProperties;
       
   132 	}
       
   133 
       
   134 
       
   135 inline TBool CPplContactsFile::IsTransactionActive() const 
       
   136 	{
       
   137 	return iActive;
       
   138 	}
       
   139 
       
   140 
       
   141 inline TBool CPplContactsFile::IsOpened() const
       
   142 	{
       
   143 	return iFileIsOpen;
       
   144 	}
       
   145 
       
   146 
       
   147 inline MLplPersistenceBroker& CPplContactsFile::PersistenceBroker()
       
   148 	{
       
   149 	return *(static_cast<MLplPersistenceBroker*>(iItemManager));
       
   150 	}
       
   151 
       
   152 inline MLplTransactionManager& CPplContactsFile::TransactionManager()
       
   153 	{
       
   154 	return *this;
       
   155 	}
       
   156 
       
   157 inline MLplFieldMatcher& CPplContactsFile::FieldMatcher()
       
   158 	{
       
   159 	return *(static_cast<MLplFieldMatcher*>(iItemManager));
       
   160 	}
       
   161 
       
   162 // Forward class reference.
       
   163 class CPrivFindViewColSet;
       
   164 
       
   165 
       
   166 /**
       
   167 The CPlCollection class provides the implementation for APIs making simple
       
   168 queries to the database as well as the functions for legacy finding and sorting
       
   169 APIs.
       
   170 */
       
   171 class NONSHARED CPlCollection : public CBase , public MLplCollection
       
   172 	{
       
   173 public:
       
   174 	static CPlCollection* NewL(CPplContactsFile& aContactsFile);
       
   175 	~CPlCollection();
       
   176 
       
   177 	CContactIdArray* CollectionL(TLplViewType aViewType,TTime aTime = 0, const TDesC& aGuid = KNullDesC);
       
   178 
       
   179 	TInt ContactCountL();
       
   180 	TBool ContactMatchesHintFieldL(TInt aBitWiseFilter, TContactItemId aContactId);	
       
   181 	CContactIdArray* MatchPhoneNumberL(const TDesC& aNumber, const TInt aMatchLengthFromRight);
       
   182 
       
   183 	CContactIdArray* FindL(const TDesC& aText, const CContactItemFieldDef* aFieldDef, TUint aSessionId);	
       
   184 	void FindAsyncInitL(const TDesC& aText,CContactItemFieldDef* aFieldDef);
       
   185 	void FindAsyncTextDefInitL(const CDesCArray& aWords,CContactTextDef* aTextDef);
       
   186 	CContactIdArray* FindAsyncL(TBool& aMoreToGo, TUint aSessionId);
       
   187 	void Reset();
       
   188 
       
   189 	TBool UsesIdentityFieldsOnly(TInt aFindFlags);
       
   190 	void ConstructBitwiseFlagsFromTextDef(TInt& aFindFlags,TInt& aIdentityColumnsCount,const CContactTextDef* aTextDef);
       
   191 	TBool SeekContactL(TContactItemId aReqId,TContactItemId& aId,TUid& aContactType, TBool& aDeleted);	
       
   192 	
       
   193 private:
       
   194     CPlCollection(CPplContactsFile& aContactsFile);
       
   195 	void ConstructL();
       
   196 	CContactIdArray* GuidL(const TDesC& aGuid);
       
   197 	CContactIdArray* ChangedSinceL(TTime aTime);
       
   198 	CContactIdArray* UnfiledL();
       
   199 	CContactIdArray* DeletedL();
       
   200 	TBool PerformFindIterationL(CContactIdArray *aIdsFound, const TDesC& aText, RSqlStatement aStatement, TInt aFieldsToSearch, TUint aSessionId);
       
   201 	TBool PerformIdFindIterationL(CContactIdArray *aIdsFound, RSqlStatement aStatement);
       
   202 	TBool FindL(CContactIdArray *aIdsFound, const TDesC& aText,const CContactItemFieldDef *aFieldDef, RSqlStatement aStatement, TUint aSessionId);
       
   203 	CContactIdArray* FilterDatabaseL(CCntFilter& aFilter);
       
   204 
       
   205 	TInt MaximumSizeOfIdentitySearchSyntax();
       
   206 	TInt ApproximateSizeOfSearchString();
       
   207 	void doAppendFieldsToSearchString(HBufC* aOrderFields) const;
       
   208 	TBool GetContactIdsForTextDefFindL(CContactIdArray* aIdArray, TUint aSessionId);
       
   209 	TBool PerformTextDefFindIterationL(CContactIdArray* aIdArray); 
       
   210 	TBool HintFieldMatchesFilter(TInt aHintField, TInt aFilter);
       
   211 	
       
   212 private:
       
   213 	enum TAsyncFindState
       
   214 		{
       
   215 		EFindInBlobFinished				=0x00000001,
       
   216 		EFindInIdentityFinished			=0x00000002,
       
   217 		EFindInEmailFinished			=0x00000004,
       
   218 		EFindInTextDefFinished			=0x00000008,
       
   219 		EFindInSIPFinished				=0x00000010
       
   220 		};
       
   221 
       
   222 private:
       
   223 	CPplContactsFile&		iContactsFile; // doesn't own
       
   224 	CContactItemFieldDef*	iFieldDef;
       
   225 	CContactTextDef*		iTextDef;
       
   226 	HBufC					*iText;
       
   227 	HBufC					*iOriginalText;
       
   228 	CDesCArray* 			iFindWords;
       
   229 	CDesCArray*				iFindWords2;
       
   230 	TInt					iFindFlags;
       
   231 	TInt					iNoIdentitySearchColumns;
       
   232 	TInt 					iFindState;
       
   233 	// The column number of the parent ID in iEmailFindView.
       
   234 	
       
   235 	CCntSqlStatement*		iSelectStatement;	
       
   236 	
       
   237 	//RSqlstatements for async find
       
   238 	RSqlStatement			selectBlobStatement;
       
   239 	RSqlStatement			selectIdentityStatement;
       
   240 	RSqlStatement			selectEmailStatement;
       
   241 	RSqlStatement			selectSIPStatement;
       
   242 	RSqlStatement			selectIdFromIdentityStatement;
       
   243 	
       
   244 	//The flag for RSqlstatements
       
   245     TBool iRSqlstatementsWorking;
       
   246 	};
       
   247 
       
   248 // Forward class reference.
       
   249 class CCntPplViewManager;
       
   250 
       
   251 /**
       
   252 This is the server-side implementation of the MLplPersistenceLayerFactory
       
   253 interface.  It is the single access point for all data services required for
       
   254 Local View functionality.
       
   255 */
       
   256 class NONSHARED CLplPersistenceLayerFactory : public CBase, public MLplPersistenceLayerFactory
       
   257 	{
       
   258 public:
       
   259 	CLplPersistenceLayerFactory(CPplContactsFile& aContactsFile,CLplContactProperties& aContactProperties);
       
   260 	~CLplPersistenceLayerFactory();
       
   261 	
       
   262 	MLplViewIteratorManager& GetViewIteratorManagerL();
       
   263 	MContactSynchroniser& GetContactSynchroniserL(TUint aSessionId);
       
   264 	MLplCollection&			GetCollectorL();	
       
   265 
       
   266 private:
       
   267 	CPplContactsFile&		iContactsFile;
       
   268 	CLplContactProperties&  iContactProperties;
       
   269 	CCntPplViewManager*	    iManager;
       
   270 	CPlCollection*			iCollection;
       
   271 	};
       
   272 
       
   273 /**
       
   274 Support class useful for testing of the Contacts Model.
       
   275 */
       
   276 class NONSHARED CLplTester : public MLplPersistenceLayerTest
       
   277 	{
       
   278 public:
       
   279 	CLplTester(CLplContactProperties& aProps, CPplContactsFile& aFile);
       
   280 
       
   281 	void MergeContactWithTemplateL(CContactItem& aContact, const CContactItem& aTempl, const CContactItemViewDef& aView) const; 
       
   282 	void DamageDatabaseL(TInt aSecretCode);
       
   283 
       
   284 private:
       
   285 	CLplContactProperties& iProps;
       
   286 	CPplContactsFile&	   iFile;
       
   287 	};
       
   288 
       
   289 #endif // __PERSISTENCELAYERIMPL_H__