summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntsrv/src/ccntpermanentdata.cpp
blob: 49c2fb646e8cd5f0efd5a172cc974a386fee8fce (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
/*
* 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: 
*
*/


/**
 @file
 @internalComponent
 @released
*/


#include <e32base.h>

#include "ccntrequest.h"
#include "ccntdbmanager.h"
#include "ccntdbmanagercontroller.h"
#include "ccntstatemachine.h"
#include "cntviewprivate.h"
#include "cviewsubsessions.h"
#include <cntviewstore.h>
#include "ccntpermanentdata.h"
#include "ccntlogger.h"

/**
Object factory method.
*/
CCntPermanentData* CCntPermanentData::NewL(CCntDbManagerController& aController)
	{
	CCntPermanentData* self = new (ELeave) CCntPermanentData(aController);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}


/**
First phase constructor.
*/
CCntPermanentData::CCntPermanentData(CCntDbManagerController& aController) 
: CActive(CActive::EPriorityLow), iState(EStateInitial), iController(aController)
	{
	CActiveScheduler::Add(this);
	}


/**
Second phase constructor.
*/
void CCntPermanentData::ConstructL()
	{
	}

	
/**
Object destructor.
*/	
CCntPermanentData::~CCntPermanentData()
	{
	Cancel();
	Close();
	}

	
/*
*/
void CCntPermanentData::Close()
	{
	iViewDefs.ResetAndDestroy();
	for (TInt i = 0; i < iViews.Count(); i++)
		{
		iViews[i]->Close(*this);
		}
	iViews.Close();
	if (iManager)
		{
		iController.CloseDatabase(*iManager);
		iManager = NULL;
		}	
	}


/**
The permanent data is created in a number of asynchronous steps. The AO uses
a simple state machine to control the steps:

1. Open the default database - EStateOpeningDb.
2. Open each default view one at a time - EStateOpeningView.
   (Views are created one at a time to avoid thrashing the dbms/file server
   with multiple requests for data that is not contiguous.)
3. Once all views are opened, or if there is an error, or the AO is cancelled -
   EStateFinished.
*/	
void CCntPermanentData::RunL()
	{
	if (iStatus.Int() == KErrNone)
		{
		switch (iState)
			{
			case EStateOpeningDb:
				{
				if (OpenNextViewL())
					{
					iState = EStateOpeningView;
					iStatus = KRequestPending;
					SetActive();
					}
				else
					{
					iState = EStateFinished;
					}
				break;
				}
			case EStateOpeningView:
				{
				if (OpenNextViewL())
					{
					iState = EStateOpeningView;
					iStatus = KRequestPending;
					SetActive();
					}
				else
					{
					iState = EStateFinished;
					}		
				break;
				}
			case EStateFinished:
				{
				// Nothing left to do
				break;
				}
			default:
				{
				__ASSERT_DEBUG(0, User::Invariant());
				break;
				}
			}
		}
	else if (iStatus.Int() == KErrNotFound)
		{
		// File could not be found to open.
		iState = EStateFinished;
		}		
	}


/**
Only need to cancel requests in certain states, otherwise there is nothing to
do.
*/	
void CCntPermanentData::DoCancel()
	{
	if (IsActive())
		{
		switch (iState)
			{
			case EStateOpeningDb:
				{
				// Cancel the asynchronous open to stop this from recieving any notifications.
				CReqCancelInternalAsyncOpen* req = NULL;

				TRAP_IGNORE(
				    {
    				// req is Pop'd from the cleanup stack to keep cleanup stack balanced
    				// before and after the TRAP.
				    req = CReqCancelInternalAsyncOpen::NewLC(0);
				    CleanupStack::Pop(req);
                    iManager->StateMachineL().ProcessRequestL(req);  // ownership transferred

	                // ProcessRequestL received ownership of the request, req can be set to 
	                // NULL
                    req = NULL;
					});

                // if request is not being cleaned up within TRAP, clean it up now!
                if (req)
                    {
                    req->Complete();
                    delete req;
                    }

				break;
				}
			case EStateOpeningView: // intentional fall-through.
			default:
				{
				// Self-complete the currently active request.
				CompleteSelf(KErrCancel);
				break;
				}
			}
		iState = EStateFinished;
		}
	}


/**
Any errors will have come from OpenNextViewL() leaving.  This is not a fatal
error for this class so do not pass it on to the scheduler.
*/
TInt CCntPermanentData::RunError(TInt /*aError*/)
	{
	iState = EStateFinished;
	return KErrNone;
	}

	
void CCntPermanentData::CompleteSelf(TInt aReason)
	{
	TRequestStatus* pStat = &iStatus;
  	User::RequestComplete(pStat, aReason);
	}


/**
Kicks off the creation of the default data. This means:

1. Opening the default database.
2. Opening any views that are specified in the default view 'store' (a CenRep
   repository).
*/	
void CCntPermanentData::StartDataCreationL()
	{
	__ASSERT_ALWAYS(!IsActive(), User::Leave(KErrInUse));
	
	TContactViewStore::GetDefaultViewsL(iViewDefs);
	
	if (iViewDefs.Count() == 0)
		{
		// No views; no need to open the default database.
		iState = EStateFinished;
		}
	else
		{
		// Only attempt to open the default file if it exists.
		if (iController.DatabaseExistsL(KNullDesC))
			{
			// Open the default database (this is asynchronous).
			CReqInternalAsyncOpen* openReq = CReqInternalAsyncOpen::NewLC(0, KNullDesC, iStatus);
			// Get an instance of the database manager for this file. One db manager is 
			// created per file and shared amoung sessions working on that file.
			iManager = iController.GetDbManagerL(openReq->FileName(), ECntFileOpen);
			// Process the open request via the state machine
			// The state machine ensures the file - owned by the manager - is in a valid
			// state to process the request.
			iManager->StateMachineL().ProcessRequestL(openReq);  // ownership transferred

	        // ProcessRequestL received ownership of the request, the request only need
	        // to be popped from CleanupStack.
			CleanupStack::Pop(openReq);

			iState = EStateOpeningDb;
			SetActive();
			}
		else
			{
			iState = EStateFinished;
			}
		}
	}


/**
Checks to see if the given CCntDbManager is being held open by this object AND
that it has a reference count of 1, i.e. this is the only object holding a
reference to it.
  
@return ETrue if this is the only object with a reference to the CCntDbManager,
EFalse otherwise.
*/
TBool CCntPermanentData::IsOnlyClientOfDbManager(CCntDbManager* manager) const
	{
	if (iManager == manager && manager->SessionCount() == 1)
		{
		return ETrue;
		}
	return EFalse;
	}


/**
Destroy any views and the manager being held by this object
*/	
void CCntPermanentData::ReleaseDbManagerL(CCntDbManager* manager)
	{
	__ASSERT_ALWAYS(manager == iManager, User::Leave(KErrArgument));
	Close();
	}


/**
Creates the next view from the list of view definitions.  The actual
construction of the view is done asynchronously.  When it has finished this
object is notified by a call to HandleContactViewEvent().
  
@return EFalse if no view was created, ETrue otherwise.
*/
TBool CCntPermanentData::OpenNextViewL()
	{
	if (iViewDefs.Count() == 0)
		{
		return EFalse;
		}

	TBool viewCreated(EFalse);
	do 
		{		
		CContactDefaultViewDefinition* viewDef = iViewDefs[0];
		CleanupStack::PushL(viewDef);
		iViewDefs.Remove(0);
		
		// Attempt to create a view from the definition.
		// If this leaves, the do..while ensures that
		// an attempt to create the next view will happen.
		CContactViewBase* view = NULL;
		TRAPD(err, view = OpenViewFromDefinitionL(*viewDef));
		if (err == KErrNone)
			{
			iViews.AppendL(view);
			viewCreated = ETrue;
			}
		else
			{
  

//		 Log the details of the view that failed to be created.
            DEBUG_PRINTF1(__VERBOSE_DEBUG__,"[CNTMODEL] CCntPermanentData::OpenNextViewL(): Failed to create a view:\n");
            DEBUG_PRINTF2(__VERBOSE_DEBUG__,"[CNTMODEL] View type: %d\n", viewDef->ViewType());
            DEBUG_PRINTF2(__VERBOSE_DEBUG__,"[CNTMODEL] View preferences: %d\n", viewDef->ViewPreferences());
			DEBUG_PRINT2(__VERBOSE_DEBUG__,_L("[CNTMODEL] View name: %S\n"), &viewDef->ViewNameL());
			DEBUG_PRINT2(__VERBOSE_DEBUG__,_L("[CNTMODEL] Sort plugin name: %S\n"), &viewDef->SortPluginNameL());
			DEBUG_PRINTF1(__VERBOSE_DEBUG__,"[CNTMODEL] View field sort order: ");
			RContactViewSortOrder sortOrder = viewDef->SortOrder();
			for (TInt i = 0; i < sortOrder.Count(); i++)
				{
				DEBUG_PRINTF2(__VERBOSE_DEBUG__,"[CNTMODEL] 0x%x, ", sortOrder[i].iUid);
				}
			RDebug::Printf("\n");

			}
		
		CleanupStack::PopAndDestroy(viewDef);
		}
	while (!viewCreated && (iViewDefs.Count() > 0));
	return viewCreated;
	}

	
CContactViewBase* CCntPermanentData::OpenViewFromDefinitionL(const CContactDefaultViewDefinition& aViewDef)
	{
	CContactViewBase* view = NULL;
	switch (aViewDef.ViewType())
		{
		case CContactDefaultViewDefinition::ERemoteView:
			{
			view = &iManager->ViewManagerL().OpenViewL(aViewDef.SortOrder(), *this, 
														aViewDef.ViewPreferences(), 
														KNullUid, aViewDef.SortPluginNameL());
			break;
			}
		case CContactDefaultViewDefinition::ENamedRemoteView:
			{
			view = &iManager->ViewManagerL().OpenNamedViewL(aViewDef.ViewNameL(), 
															aViewDef.SortOrder(), *this, 
															aViewDef.ViewPreferences(), 
															KNullUid, aViewDef.SortPluginNameL());
			break;
			}
		default:
			{
			User::Leave(KErrArgument);
			}
		}
	return view;	
	}


void CCntPermanentData::HandleContactViewEvent(const CContactViewBase& /*aView*/, const TContactViewEvent& /*aEvent*/)
	{
	// Complete the active object regardless of whether the view was 
	// created successfully or not. This class is only trying to pre-create 
	// the view(s) for the main Contacts application. If there are errors 
	// creating the view then the application should deal with them 
	// (as it would if this class didn't exist).
	if (iState == EStateOpeningView)
		{
		CompleteSelf(KErrNone);
		}
	}