phonebookengines/contactsmodel/cntplsql/inc/persistencelayerimpl.h
changeset 0 e686773b3f54
child 6 e8e3147d53eb
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 private: // General methods used to implement all interfaces.
       
    80 	CPplContactsFile(CLplContactProperties& aProps, MContactDbObserver* aObserver);
       
    81 	void ConstructL();
       
    82 
       
    83 	void GenerateNotificationEventL(TBool aNotify = EFalse);
       
    84 
       
    85 	void GetPhysicalPathL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName);
       
    86 	void GetPhysicalFileNameL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName);
       
    87 
       
    88 	inline void LocalFsL();
       
    89 
       
    90 private: // Member variables used by several interfaces.
       
    91 	TBool iActive;  // Member variables used by MLplTransactionManager interface.
       
    92 
       
    93 	RFs iLocalFs;
       
    94 	
       
    95 	CPplContactItemManager* iItemManager;
       
    96   	MContactDbObserver* iDbObserver;  
       
    97   	CLplContactProperties& iContactProperties;
       
    98 
       
    99 	RSqlDatabase iDatabase;     // owned
       
   100 	TDriveUnit iDatabaseDrive;
       
   101 	TBool iFileIsOpen;
       
   102 	
       
   103 	RPplIccContactStore iIccContactStore; //owned
       
   104 	HBufC8* iConfigureStr;
       
   105 	};
       
   106 
       
   107 
       
   108 inline void CPplContactsFile::LocalFsL()
       
   109   	{
       
   110   	if(iLocalFs.Handle() == 0)
       
   111   		{
       
   112   		User::LeaveIfError(iLocalFs.Connect());
       
   113   		}
       
   114   	}
       
   115 
       
   116 
       
   117 inline RSqlDatabase& CPplContactsFile::NamedDatabase()
       
   118 	{
       
   119 	return iDatabase;
       
   120 	}
       
   121 
       
   122 
       
   123 inline CLplContactProperties& CPplContactsFile::ContactProperties()
       
   124 	{
       
   125 	return iContactProperties;
       
   126 	}
       
   127 
       
   128 
       
   129 inline TBool CPplContactsFile::IsTransactionActive() const 
       
   130 	{
       
   131 	return iActive;
       
   132 	}
       
   133 
       
   134 
       
   135 inline TBool CPplContactsFile::IsOpened() const
       
   136 	{
       
   137 	return iFileIsOpen;
       
   138 	}
       
   139 
       
   140 
       
   141 inline MLplPersistenceBroker& CPplContactsFile::PersistenceBroker()
       
   142 	{
       
   143 	return *(static_cast<MLplPersistenceBroker*>(iItemManager));
       
   144 	}
       
   145 
       
   146 inline MLplTransactionManager& CPplContactsFile::TransactionManager()
       
   147 	{
       
   148 	return *this;
       
   149 	}
       
   150 
       
   151 inline MLplFieldMatcher& CPplContactsFile::FieldMatcher()
       
   152 	{
       
   153 	return *(static_cast<MLplFieldMatcher*>(iItemManager));
       
   154 	}
       
   155 
       
   156 // Forward class reference.
       
   157 class CPrivFindViewColSet;
       
   158 
       
   159 
       
   160 /**
       
   161 The CPlCollection class provides the implementation for APIs making simple
       
   162 queries to the database as well as the functions for legacy finding and sorting
       
   163 APIs.
       
   164 */
       
   165 class NONSHARED CPlCollection : public CBase , public MLplCollection
       
   166 	{
       
   167 public:
       
   168 	static CPlCollection* NewL(CPplContactsFile& aContactsFile);
       
   169 	~CPlCollection();
       
   170 
       
   171 	CContactIdArray* CollectionL(TLplViewType aViewType,TTime aTime = 0, const TDesC& aGuid = KNullDesC);
       
   172 
       
   173 	TInt ContactCountL();
       
   174 	TBool ContactMatchesHintFieldL(TInt aBitWiseFilter, TContactItemId aContactId);	
       
   175 	CContactIdArray* MatchPhoneNumberL(const TDesC& aNumber, const TInt aMatchLengthFromRight);
       
   176 
       
   177 	CContactIdArray* FindL(const TDesC& aText, const CContactItemFieldDef* aFieldDef, TUint aSessionId);	
       
   178 	void FindAsyncInitL(const TDesC& aText,CContactItemFieldDef* aFieldDef);
       
   179 	void FindAsyncTextDefInitL(const CDesCArray& aWords,CContactTextDef* aTextDef);
       
   180 	CContactIdArray* FindAsyncL(TBool& aMoreToGo, TUint aSessionId);
       
   181 
       
   182 	TBool UsesIdentityFieldsOnly(TInt aFindFlags);
       
   183 	void ConstructBitwiseFlagsFromTextDef(TInt& aFindFlags,TInt& aIdentityColumnsCount,const CContactTextDef* aTextDef);
       
   184 	TBool SeekContactL(TContactItemId aReqId,TContactItemId& aId,TUid& aContactType, TBool& aDeleted);	
       
   185 	
       
   186 private:
       
   187     CPlCollection(CPplContactsFile& aContactsFile);
       
   188 	void ConstructL();
       
   189 	CContactIdArray* GuidL(const TDesC& aGuid);
       
   190 	CContactIdArray* ChangedSinceL(TTime aTime);
       
   191 	CContactIdArray* UnfiledL();
       
   192 	CContactIdArray* DeletedL();
       
   193 	TBool PerformFindIterationL(CContactIdArray *aIdsFound, const TDesC& aText, RSqlStatement aStatement, TInt aFieldsToSearch, TUint aSessionId);
       
   194 	TBool PerformIdFindIterationL(CContactIdArray *aIdsFound, RSqlStatement aStatement);
       
   195 	TBool FindL(CContactIdArray *aIdsFound, const TDesC& aText,const CContactItemFieldDef *aFieldDef, RSqlStatement aStatement, TUint aSessionId);
       
   196 	CContactIdArray* FilterDatabaseL(CCntFilter& aFilter);
       
   197 	void Reset();
       
   198 	TInt MaximumSizeOfIdentitySearchSyntax();
       
   199 	TInt ApproximateSizeOfSearchString();
       
   200 	void doAppendFieldsToSearchString(HBufC* aOrderFields) const;
       
   201 	TBool GetContactIdsForTextDefFindL(CContactIdArray* aIdArray, TUint aSessionId);
       
   202 	TBool PerformTextDefFindIterationL(CContactIdArray* aIdArray); 
       
   203 	TBool HintFieldMatchesFilter(TInt aHintField, TInt aFilter);
       
   204 	
       
   205 private:
       
   206 	enum TAsyncFindState
       
   207 		{
       
   208 		EFindInBlobFinished				=0x00000001,
       
   209 		EFindInIdentityFinished			=0x00000002,
       
   210 		EFindInEmailFinished			=0x00000004,
       
   211 		EFindInTextDefFinished			=0x00000008,
       
   212 		EFindInSIPFinished				=0x00000010
       
   213 		};
       
   214 
       
   215 private:
       
   216 	CPplContactsFile&		iContactsFile; // doesn't own
       
   217 	CContactItemFieldDef*	iFieldDef;
       
   218 	CContactTextDef*		iTextDef;
       
   219 	HBufC					*iText;
       
   220 	HBufC					*iOriginalText;
       
   221 	CDesCArray* 			iFindWords;
       
   222 	CDesCArray*				iFindWords2;
       
   223 	TInt					iFindFlags;
       
   224 	TInt					iNoIdentitySearchColumns;
       
   225 	TInt 					iFindState;
       
   226 	// The column number of the parent ID in iEmailFindView.
       
   227 	
       
   228 	CCntSqlStatement*		iSelectStatement;	
       
   229 	
       
   230 	//RSqlstatements for async find
       
   231 	RSqlStatement			selectBlobStatement;
       
   232 	RSqlStatement			selectIdentityStatement;
       
   233 	RSqlStatement			selectEmailStatement;
       
   234 	RSqlStatement			selectSIPStatement;
       
   235 	RSqlStatement			selectIdFromIdentityStatement;
       
   236 	};
       
   237 
       
   238 // Forward class reference.
       
   239 class CCntPplViewManager;
       
   240 
       
   241 /**
       
   242 This is the server-side implementation of the MLplPersistenceLayerFactory
       
   243 interface.  It is the single access point for all data services required for
       
   244 Local View functionality.
       
   245 */
       
   246 class NONSHARED CLplPersistenceLayerFactory : public CBase, public MLplPersistenceLayerFactory
       
   247 	{
       
   248 public:
       
   249 	CLplPersistenceLayerFactory(CPplContactsFile& aContactsFile,CLplContactProperties& aContactProperties);
       
   250 	~CLplPersistenceLayerFactory();
       
   251 	
       
   252 	MLplViewIteratorManager& GetViewIteratorManagerL();
       
   253 	MContactSynchroniser& GetContactSynchroniserL(TUint aSessionId);
       
   254 	MLplCollection&			GetCollectorL();	
       
   255 
       
   256 private:
       
   257 	CPplContactsFile&		iContactsFile;
       
   258 	CLplContactProperties&  iContactProperties;
       
   259 	CCntPplViewManager*	    iManager;
       
   260 	CPlCollection*			iCollection;
       
   261 	};
       
   262 
       
   263 /**
       
   264 Support class useful for testing of the Contacts Model.
       
   265 */
       
   266 class NONSHARED CLplTester : public MLplPersistenceLayerTest
       
   267 	{
       
   268 public:
       
   269 	CLplTester(CLplContactProperties& aProps, CPplContactsFile& aFile);
       
   270 
       
   271 	void MergeContactWithTemplateL(CContactItem& aContact, const CContactItem& aTempl, const CContactItemViewDef& aView) const; 
       
   272 	void DamageDatabaseL(TInt aSecretCode);
       
   273 
       
   274 private:
       
   275 	CLplContactProperties& iProps;
       
   276 	CPplContactsFile&	   iFile;
       
   277 	};
       
   278 
       
   279 #endif // __PERSISTENCELAYERIMPL_H__