summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/src/cntviewstore.cpp
blob: d85bff2a63567c95293d6425534d7b3ff89abfdd (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* 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: 
*
*/


#include <e32base.h>
#include <centralrepository.h>
#include <cntdb.h>
#include "cntstd.h"
#include "rcntmodel.h"
#include <cntviewbase.h>
#include <cntviewstore.h>
#include "cntviewstoreprivate.h"


/*
  Constant values used to access the view definition data 
  in the central repository.
*/
//const TUint32 KTableMask = 0x00FF0000;
const TUint32 KRowMask = 0x0000FF00;
const TUint32 KColumnMask = 0x000000FF;

const TUint32 KViewDefinitionsTable = 0x00010000;
const TUint32 KViewSortOrderTable = 0x00020000;
const TUint32 KTypeCol = 0x00000000;
const TUint32 KNameCol = 0x00000001;
const TUint32 KSortPluginCol = 0x00000002;
const TUint32 KPreferencesCol = 0x00000003;
const TUint32 KSortOrderIdxCol = 0x00000004;

const TUid KContactsRepositoryUid = { 0x10003A73 };

/** 
	Stores a list of default remote view definitions in the contacts servers' 
    persistent data store. The view definitions are assumed to relate to 
    the default contacts database although this is not enforced.

@param aDefaultViews Array of view definitions to store

@publishedPartner
@released
*/
EXPORT_C void TContactViewStore::SetDefaultViewsL(const RPointerArray<CContactDefaultViewDefinition> &aDefaultViews)
	{
	CRepository* rep = CRepository::NewLC(KContactsRepositoryUid);
	
	// Attempt to store in one transaction; if it fails, pass
	// the error on as a leave.
	rep->StartTransaction(CRepository::EConcurrentReadWriteTransaction);

	// clear out any existing entries
	TUint32 errorKey;
	rep->Delete(KViewDefinitionsTable, ~(KRowMask | KColumnMask), errorKey);
	rep->Delete(KViewSortOrderTable, ~(KRowMask | KColumnMask), errorKey);
	
	// for each view definition
	TInt count = aDefaultViews.Count();
	for (TInt i = 0; i < count; i++)
		{
		CContactDefaultViewDefinition* def = aDefaultViews[i];

		// View Type
		TUint32 key = (KViewDefinitionsTable | (i << 8) | KTypeCol);
		rep->Set(key, def->ViewType());
				
		// View Name
		key = (KViewDefinitionsTable | (i << 8) | KNameCol);
		rep->Set(key, def->ViewNameL());
		
		// Sort Plugin Name
		key = (KViewDefinitionsTable | (i << 8) | KSortPluginCol);
		rep->Set(key, def->SortPluginNameL());

		// View Preferences
		key = (KViewDefinitionsTable | (i << 8) | KPreferencesCol);
		rep->Set(key, def->ViewPreferences());
		
		// Sort Order Row Index
		key = (KViewDefinitionsTable | (i << 8) | KSortOrderIdxCol);
		rep->Set(key, i);
		
		// Write the entries for the sort order in the sort order table
		// Need Assert that Count() <= 256 (i.e. 0xFF)
		TInt fieldCount = def->SortOrder().Count();
		for (TInt j = 0; j < fieldCount; j++)
			{
			key = (KViewSortOrderTable | (i << 8) | j);
			rep->Set(key, static_cast<TInt>(def->SortOrder()[j].iUid));
			}
		}

	User::LeaveIfError(rep->CommitTransaction(errorKey));
		
	CleanupStack::PopAndDestroy(rep);	
	}

/** 
	Retrieves the list of default view definitions stored in the contacts
    servers' persistent data storage.
    
@publishedPartner
@released
*/	
EXPORT_C void TContactViewStore::GetDefaultViewsL(RPointerArray<CContactDefaultViewDefinition> &aDefaultViews)
	{
	__ASSERT_ALWAYS(aDefaultViews.Count() == 0, User::Leave(KErrArgument));
	
	CRepository* rep = CRepository::NewLC(KContactsRepositoryUid);
	
	TInt type(1);
	TInt prefs(0);
	TInt sortOrderRow;
	RContactViewSortOrder viewSortOrder;
	CleanupClosePushL(viewSortOrder);
	TInt actualLength;
	RBuf name;
	name.CleanupClosePushL();
	RBuf8 pluginName;
	pluginName.CleanupClosePushL();	
	
	// Attempt to store in one transaction; if it fails, pass
	// the error on as a leave.
	rep->StartTransaction(CRepository::EConcurrentReadWriteTransaction);

	TInt row(0);
	const TUint32 KMask = ~KColumnMask;
	TUint32 partialKey = (KViewDefinitionsTable | (row << 8));
	RArray<TUint32> foundKeys;
	CleanupClosePushL(foundKeys);
	CContactDefaultViewDefinition* viewDef;
		
	TInt err = rep->FindL(partialKey, KMask, foundKeys);
	while (err != KErrNotFound)
		{
		TInt count = foundKeys.Count();		
		for (TInt col = 0; col < count; col++)
			{
			switch (foundKeys[col] & KColumnMask)
				{
				case KTypeCol:
					{
					err = rep->Get(foundKeys[col], type);
					break;
					}				
				case KNameCol:
					{
					err = rep->Get(foundKeys[col], name, actualLength);
					if (err == KErrOverflow)
						{
						name.ReAllocL(actualLength);
						rep->Get(foundKeys[col], name);
						}
					break;
					}
				case KSortPluginCol:
					{
					err = rep->Get(foundKeys[col], pluginName, actualLength);
					if (err == KErrOverflow)
						{
						pluginName.ReAllocL(actualLength);
						rep->Get(foundKeys[col], pluginName);
						}							
					break;
					}
				case KPreferencesCol:
					{
					rep->Get(foundKeys[col], prefs);
					break;
					}
				case KSortOrderIdxCol:
					{
					rep->Get(foundKeys[col], sortOrderRow);
					
					// read in the sort order from the sort order table
					RArray<TUint32> foundFieldKeys;
					partialKey = (KViewSortOrderTable | (sortOrderRow << 8));
					err = rep->FindL(partialKey, KMask, foundFieldKeys);
					CleanupClosePushL(foundFieldKeys);
					for (TInt field = 0; field < foundFieldKeys.Count(); field++)
						{
						TInt fieldUid;
						rep->Get(foundFieldKeys[field], fieldUid);
						viewSortOrder.AppendL(TUid().Uid(fieldUid));
						}
					CleanupStack::PopAndDestroy(&foundFieldKeys);
					break;
					}
				default:
					{
					break;
					}
				}
			}
													   
		viewDef = CContactDefaultViewDefinition::NewLC(static_cast<CContactDefaultViewDefinition::TViewType>(type),
													   name, viewSortOrder, 
													   static_cast<TContactViewPreferences>(prefs), 
													   pluginName);													   
			
		aDefaultViews.AppendL(viewDef);
		CleanupStack::Pop(viewDef);	
		viewSortOrder.Close();

		row++;
		partialKey = (KViewDefinitionsTable | (row << 8));
		err = rep->FindL(partialKey, KMask, foundKeys);
		}

	TUint32 errorKey;
	User::LeaveIfError(rep->CommitTransaction(errorKey));

	CleanupStack::PopAndDestroy(5, rep); // foundKeys, pluginName, name, viewSortOrder, rep
	}
	
/**
  Returns an array of definitions describing the remote views that exist 
  inside the server for the given contact database.
  Note: The order of views in the array is not guaranteed to be the order 
  that they were created in.

@param aDbName Name of the contacts database to query for open views.
@param aViewDefs An empty array that will be filled with any exisiting
                 open views for the given contact database.

@internalTechnology
@test
*/
#if defined(_DEBUG)
EXPORT_C void TContactViewStorePrivate::GetDefinitionsOfExistingViewsL(const TDesC& aDbName, RPointerArray<CContactDefaultViewDefinition>& aViewDefs)
	{
	RCntModel session;
	CleanupClosePushL(session);
	session.ConnectL();
	session.GetDefinitionsOfExistingViewsL(aDbName, aViewDefs);
	CleanupStack::PopAndDestroy(); // session
	}
#else
EXPORT_C void TContactViewStorePrivate::GetDefinitionsOfExistingViewsL(const TDesC& , RPointerArray<CContactDefaultViewDefinition>& )
	{
	}
#endif // _DEBUG
	
/**  Static constructor. Returns a default remote view definition.

@param	aViewType	Type of the view. 
@param	aViewName	The name of the view. Ignored if aViewType = ERemoteView; 
@param 	aSortOrder	Specifies the fields to use to sort the items in the view.
@param 	aContactTypes	Specifies which types of contact items should be included in the
view and the behaviour for items that do not have content in any of the fields specified in the sort order.
@param 	aSortPluginName	Specifies a plug-in that will be used to compare view contacts
when the the view is sorted. This name is used by ECOM to select the plugin, and is matched
with the "default_data" of all ECOM plugins that support the required interface.
@return The newly constructed object.

@publishedPartner
@released
*/
EXPORT_C  CContactDefaultViewDefinition* CContactDefaultViewDefinition::NewLC(TViewType aViewType, const TDesC &aViewName, const RContactViewSortOrder &aSortOrder, TContactViewPreferences aContactTypes, const TDesC8 &aSortPluginName)
	{
	CContactDefaultViewDefinition* self = new (ELeave) CContactDefaultViewDefinition(aViewType, aContactTypes);
	CleanupStack::PushL(self);
	self->ConstructL(aViewName, aSortOrder, aSortPluginName);
	return self;	
	}

/**  Static constructor. Returns a default remote view definition created from the contents
     of the given stream.

@param aStream Stream which the parmameters for the view definition are read from

@return The newly constructed object.

@publishedPartner
@released
*/
EXPORT_C  CContactDefaultViewDefinition* CContactDefaultViewDefinition::NewLC(RReadStream& aStream)
	{
	CContactDefaultViewDefinition* self = new (ELeave) CContactDefaultViewDefinition;
	CleanupStack::PushL(self);
	aStream >> *self;
	return self;	
	}
	
CContactDefaultViewDefinition::CContactDefaultViewDefinition(TViewType aViewType, TContactViewPreferences aContactTypes):
	iContactTypes(aContactTypes),
	iViewType(aViewType)
	{
	}
	
CContactDefaultViewDefinition::CContactDefaultViewDefinition()
	{
	}
	
void CContactDefaultViewDefinition::ConstructL(const TDesC &aViewName, const RContactViewSortOrder &aSortOrder, const TDesC8 &aSortPluginName)
	{
	iViewName = aViewName.AllocL();
	iSortPluginName = aSortPluginName.AllocL();
	iSortOrder.CopyL(aSortOrder);
	}	

/**
@publishedPartner
@released
*/
EXPORT_C CContactDefaultViewDefinition::~CContactDefaultViewDefinition()
	{
	Close();
	}
	
void CContactDefaultViewDefinition::Close()
	{
	iSortOrder.Close();
	delete iSortPluginName;
	iSortPluginName = NULL;
	delete iViewName;	
	iViewName = NULL;
	}

/** Gets the view name.
@return The view name.

@publishedPartner
@released
*/
EXPORT_C const TDesC& CContactDefaultViewDefinition::ViewNameL() const
	{ return *iViewName; }

/** Gets the sort plugin name.
@return The sort plugin name.

@publishedPartner
@released
*/
EXPORT_C const TDesC8& CContactDefaultViewDefinition::SortPluginNameL() const
	{ return *iSortPluginName; }

/** Gets the sort order.
@return The sort order.

@publishedPartner
@released
*/
EXPORT_C const RContactViewSortOrder& CContactDefaultViewDefinition::SortOrder() const
	{ return iSortOrder; }

/** Gets the view preferences.
@return The view preferences.

@publishedPartner
@released
*/
EXPORT_C TContactViewPreferences CContactDefaultViewDefinition::ViewPreferences() const
	{ return iContactTypes; }

/** Gets the view type.
@return The view type.

@publishedPartner
@released
*/
EXPORT_C CContactDefaultViewDefinition::TViewType CContactDefaultViewDefinition::ViewType() const
	{ return iViewType; }

/** Standard object internalization function.
@param aStream Stream from which the object is internalized.

@publishedPartner
@released
*/
EXPORT_C void CContactDefaultViewDefinition::InternalizeL(RReadStream &aStream)
	{
	// free any existing data
	Close();
	
	// read in the new data
	const TInt KMaxDescriptorLength(KMaxTInt);
	iViewName = HBufC::NewL(aStream, KMaxDescriptorLength);
	iSortPluginName = HBufC8::NewL(aStream, KMaxDescriptorLength);
	aStream >> iSortOrder;
	iContactTypes = static_cast<TContactViewPreferences>(aStream.ReadUint32L());
	iViewType = static_cast<TViewType>(aStream.ReadUint32L());
	}

/** Standard object externalization function.
@param aStream Stream that the object is externalized to.

@publishedPartner
@released
*/
EXPORT_C void CContactDefaultViewDefinition::ExternalizeL(RWriteStream &aStream) const
	{
	aStream << *iViewName;
	aStream << *iSortPluginName;
	aStream << iSortOrder;
	aStream.WriteUint32L(static_cast<TUint32>(iContactTypes));
	aStream.WriteUint32L(static_cast<TUint32>(iViewType));
	}