summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntplsql/inc/persistencelayerimpl.h
blob: ae3087ce80904983df650aed7af19ad588497706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/




/**
 @file
 @internalComponent
 @released
*/

#ifndef __PERSISTENCELAYERIMPL_H__
#define __PERSISTENCELAYERIMPL_H__

#include "persistencelayer.h" // for interface definitions.
#include "pplcontactitemmanager.h"
#include "pltables.h"
#include <sqldb.h>
class CContactItemManger;
class CCntSqlStatement;

#define KIgnorePhbkSync 0

/**
The CPplContactsFile class represents a single contacts database file and
provides all common database file operations such as open, close, create and
delete. 
*/
class NONSHARED CPplContactsFile:  public CBase, public MLplContactsFile, public MLplTransactionManager
	{
public: // MLplContactsFile interface.

	static CPplContactsFile* NewL(CLplContactProperties& aProps, MContactDbObserverV2* aObserver);
	~CPplContactsFile();

	IMPORT_C void CreateL(const TDesC& aFileName, TPlCreateMode aMode = EPlLeaveIfExist);
	void OpenL(const TDesC& aFileName, TBool aNotify = EFalse);  
	void Close(TBool aNotify = EFalse);
	void DeleteL(const TDesC& aFileName);
	CDesCArray* ListL(TDriveUnit* aDriveUnit = NULL);
	inline TBool IsOpened() const;
	virtual void CloseTablesL(TBool aNotify);
	virtual void OpenTablesL(TBool aNotify = EFalse);	

	inline MLplPersistenceBroker& PersistenceBroker();
	inline MLplTransactionManager& TransactionManager();
	inline MLplFieldMatcher& FieldMatcher();
	
	void DatabaseDrive(TDriveUnit& aDriveUnit);
	TBool DatabaseExistsL(const TDesC& aFileName);
	TInt FileSize() const;
	
	// Non-interface methods.
	void RegisterDbObserver(MContactDbObserverV2& aDbObserver);
	inline RSqlDatabase& NamedDatabase();
	inline CLplContactProperties& ContactProperties();

	void StartTransactionL();
	void CommitCurrentTransactionL(TUint aSessionId);
	void RollbackCurrentTransactionL(TUint aSessionId);
	inline TBool IsTransactionActive() const;

	void NotifyObserver() const;
	
    void AddSqlDBObserverL( MLplSqlDatabaseObserver& aSqlDatabaseObserver );
    void RemoveSqlDBObserverL( MLplSqlDatabaseObserver& aSqlDatabaseObserver );

private: // General methods used to implement all interfaces.
	CPplContactsFile(CLplContactProperties& aProps, MContactDbObserverV2* aObserver);
	void ConstructL();

	void GenerateNotificationEventL(TBool aNotify = EFalse);

	void GetPhysicalPathL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName);
	void GetPhysicalFileNameL(TDes& aPhysicalFileName, const TDesC& aSecuredFileName);


	inline void LocalFsL();

private: // Member variables used by several interfaces.
	TBool iActive;  // Member variables used by MLplTransactionManager interface.

	RFs iLocalFs;
	
	CPplContactItemManager* iItemManager;
  	MContactDbObserverV2* iDbObserver;  
  	CLplContactProperties& iContactProperties;

	RSqlDatabase iDatabase;     // owned
	TDriveUnit iDatabaseDrive;
	TBool iFileIsOpen;
	
	RPplIccContactStore iIccContactStore; //owned
	HBufC8* iConfigureStr;
	
	// The array of observers which are monitoring RSqlDatabase.
    RPointerArray<MLplSqlDatabaseObserver> iSqlDatabaseObservers;
	};


inline void CPplContactsFile::LocalFsL()
  	{
  	if(iLocalFs.Handle() == 0)
  		{
  		User::LeaveIfError(iLocalFs.Connect());
  		}
  	}


inline RSqlDatabase& CPplContactsFile::NamedDatabase()
	{
	return iDatabase;
	}


inline CLplContactProperties& CPplContactsFile::ContactProperties()
	{
	return iContactProperties;
	}


inline TBool CPplContactsFile::IsTransactionActive() const 
	{
	return iActive;
	}


inline TBool CPplContactsFile::IsOpened() const
	{
	return iFileIsOpen;
	}


inline MLplPersistenceBroker& CPplContactsFile::PersistenceBroker()
	{
	return *(static_cast<MLplPersistenceBroker*>(iItemManager));
	}

inline MLplTransactionManager& CPplContactsFile::TransactionManager()
	{
	return *this;
	}

inline MLplFieldMatcher& CPplContactsFile::FieldMatcher()
	{
	return *(static_cast<MLplFieldMatcher*>(iItemManager));
	}

// Forward class reference.
class CPrivFindViewColSet;


/**
The CPlCollection class provides the implementation for APIs making simple
queries to the database as well as the functions for legacy finding and sorting
APIs.
*/
class NONSHARED CPlCollection : public CBase , public MLplCollection
    {
public:
    static CPlCollection* NewL(CPplContactsFile& aContactsFile);
    ~CPlCollection();

    CContactIdArray* CollectionL(TLplViewType aViewType,TTime aTime = 0, const TDesC& aGuid = KNullDesC);

    TInt ContactCountL();
    TBool ContactMatchesHintFieldL(TInt aBitWiseFilter, TContactItemId aContactId);
    CContactIdArray* MatchPhoneNumberL(const TDesC& aNumber, const TInt aMatchLengthFromRight);
    CContactIdArray* FindSpeedDialContactsL();
    CContactIdArray* FindL(const TDesC& aText, const CContactItemFieldDef* aFieldDef, TUint aSessionId);
    void FindAsyncInitL(const TDesC& aText,CContactItemFieldDef* aFieldDef);
    void FindAsyncTextDefInitL(const CDesCArray& aWords,CContactTextDef* aTextDef);
    CContactIdArray* FindAsyncL(TBool& aMoreToGo, TUint aSessionId);
    void Reset();

    TBool UsesIdentityFieldsOnly(TInt aFindFlags);
    void ConstructBitwiseFlagsFromTextDef(TInt& aFindFlags,TInt& aIdentityColumnsCount,const CContactTextDef* aTextDef);
    TBool SeekContactL(TContactItemId aReqId,TContactItemId& aId,TUid& aContactType, TBool& aDeleted);    
    
private:
    CPlCollection(CPplContactsFile& aContactsFile);
    void ConstructL();
    CContactIdArray* GuidL(const TDesC& aGuid);
    CContactIdArray* ChangedSinceL(TTime aTime);
    CContactIdArray* UnfiledL();
    CContactIdArray* DeletedL();
    TBool PerformFindIterationL(CContactIdArray *aIdsFound, const TDesC& aText, RSqlStatement aStatement, TInt aFieldsToSearch, TUint aSessionId);
    TBool PerformIdFindIterationL(CContactIdArray *aIdsFound, RSqlStatement aStatement);
    TBool FindL(CContactIdArray *aIdsFound, const TDesC& aText,const CContactItemFieldDef *aFieldDef, RSqlStatement aStatement, TUint aSessionId);
    void  FindIMPPContactsL(CContactIdArray *aIdsFound, RSqlStatement aStatement);
    CContactIdArray* FilterDatabaseL(CCntFilter& aFilter);

    TInt MaximumSizeOfIdentitySearchSyntax();
    TInt ApproximateSizeOfSearchString();
    void doAppendFieldsToSearchString(HBufC* aOrderFields) const;
    TBool GetContactIdsForTextDefFindL(CContactIdArray* aIdArray, TUint aSessionId);
    TBool PerformTextDefFindIterationL(CContactIdArray* aIdArray); 
    TBool HintFieldMatchesFilter(TInt aHintField, TInt aFilter);
    
private:
    enum TAsyncFindState
        {
        EFindInBlobFinished             =0x00000001,
        EFindInIdentityFinished         =0x00000002,
        EFindInEmailFinished            =0x00000004,
        EFindInTextDefFinished          =0x00000008,
        EFindInSIPFinished              =0x00000010
        };

private:
    CPplContactsFile&       iContactsFile; // doesn't own
    CContactItemFieldDef*   iFieldDef;
    CContactTextDef*        iTextDef;
    HBufC                   *iText;
    HBufC                   *iOriginalText;
    CDesCArray*             iFindWords;
    CDesCArray*             iFindWords2;
    TInt                    iFindFlags;
    TInt                    iNoIdentitySearchColumns;
    TInt                    iFindState;
    // The column number of the parent ID in iEmailFindView.
    
    CCntSqlStatement*        iSelectStatement;
    
    //RSqlstatements for async find
    RSqlStatement            selectBlobStatement;
    RSqlStatement            selectIdentityStatement;
    RSqlStatement            selectEmailStatement;
    RSqlStatement            selectSIPStatement;
    RSqlStatement            selectIdFromIdentityStatement;
    
    //The flag for RSqlstatements
    TBool iRSqlstatementsWorking;
    };

// Forward class reference.
class CCntPplViewManager;

/**
This is the server-side implementation of the MLplPersistenceLayerFactory
interface.  It is the single access point for all data services required for
Local View functionality.
*/
class NONSHARED CLplPersistenceLayerFactory : public CBase, public MLplPersistenceLayerFactory
	{
public:
	CLplPersistenceLayerFactory(CPplContactsFile& aContactsFile,CLplContactProperties& aContactProperties);
	~CLplPersistenceLayerFactory();
	
	MLplViewIteratorManager& GetViewIteratorManagerL();
	MContactSynchroniser& GetContactSynchroniserL(TUint aSessionId);
	MLplCollection&			GetCollectorL();	

private:
	CPplContactsFile&		iContactsFile;
	CLplContactProperties&  iContactProperties;
	CCntPplViewManager*	    iManager;
	CPlCollection*			iCollection;
	};

/**
Support class useful for testing of the Contacts Model.
*/
class NONSHARED CLplTester : public MLplPersistenceLayerTest
	{
public:
	CLplTester(CLplContactProperties& aProps, CPplContactsFile& aFile);

	void MergeContactWithTemplateL(CContactItem& aContact, const CContactItem& aTempl, const CContactItemViewDef& aView) const; 
	void DamageDatabaseL(TInt aSecretCode);

private:
	CLplContactProperties& iProps;
	CPplContactsFile&	   iFile;
	};

#endif // __PERSISTENCELAYERIMPL_H__