summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntsrv/src/ccntrequest.cpp
blob: 2880ef32174594b8a87de7b19da55ee7c41e1c93 (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
/*
* 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
 @internalTechnology
 @released 
*/

#include "ccntrequest.h"
#include "ccntstatemachine.h"
#include "ccntpackager.h"
#include <cntitem.h>

// CCntRequest 
// CCntRequest is the base class for all Request classes that handle external
// requests (requests that originate from the CContactDatabase). Internal requests
// derive from the CReqInternal class. 
// Note 1: All unpacking of objects should be done within Construction.
// Note 2: The destruction of request objects is a little unusual.
//		   The destruction of requests objects is usually the responsibility of the CStatemachine or in the CRequestStore class.
//         The destruction of requests is usually performed using the CCntRequest::CompletePD method. See below
// Note 3: Each request has a timeout member - the time for which the request remains valid - and a timeouterror member variable.
//		   The iTimeOutError is the error that will be returned to the client in the event of the request timing out. The default 
//		   timeout value is KErrNotReady but the CStateMachine can reset this depending on the last action the statemachine attempted
// 		   to perform before the request timedout.
//		   Timeouts work as follows - the statemachine returns TDeferred indicating it could not process the request in the current state
//		   The request is added to the request store, CRequestStore, and the timeout is activated via the CCntRequest::ActivateTimeOutL 		
//		   method. There are two possible outcomes:
//			1. The state changes, or a contact item is unlocked, and the request is activated and processed by the CStateMachine		
//			2. The request times out, CCntRequestTimer::RunL is called and the request is completed with the iTimeOutError error code.

/** 
  Base CCntRequest constructor
  @param  aSessionId The session from which the request originated
  @param  aMessage The RMessage from the client
  @param  aTimeOut The lenght of time in milliseconds for which the request remains valid
*/

CCntRequest::CCntRequest(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
						:iMessage(aMessage),
						 iSessionId(aSessionId), 
						 iTimeOut(aTimeOut),
						 iTimeOutError(KErrNotReady)
	{
	ASSERT(aSessionId); // Should never be NULL
	// All internal requests do not have a session ID, such requests derive from the CReqInternal
	// and NOT from CCntRequest.
	}

/** 
  Complete the requests internal iMessage

  @param  aErrCode The error code with which the message will be completed
*/
void CCntRequest::Complete(TInt aErrCode)
	{
	iMessage.Complete(aErrCode); 
	}

CCntRequest::~CCntRequest()
	{
	delete iTimer;
	}

/** 
  Start the timeout 
  A timer is only ever added when it does not exist and a non-zero timeout value has
  been specified, because the request may be activated, processed and deferred more than once.
  The result of request processing could be to add the request to the request queue again. 
 
  @param  aReqStore When a times out, the timer access the store to complete the request with
  	 		a timeout error.
 */
void CCntRequest::ActivateTimeOutL(CRequestStore& aReqStore)
	{
	if ((iTimer == NULL) && (iTimeOut != 0))
		{
		iTimer = CCntRequestTimer::NewL(*this, aReqStore);
		iTimer->Start();
		}
	}

/**
  Stop the timeout
 */
void CCntRequest::DeActivateTimeOut()
	{
	if (iTimer)
		{
		iTimer->Stop();	
		}
		
	delete iTimer;
	iTimer = NULL;
	}

/**
  Set the timeout error code
  Inorder to assure binary compatibility with the original Contacts Model,
  which did not retry processing client requests, the error code - reason 
  why an request can not be processed - needs to be stored and returned to
  to the client if a subsequest/retry to process the request fails.
  For example if a contact item has been locked by one session, a request
  to open the contact item by a second session should complete with KErrInUse
  however if the server has been put into a transaction state by one session,
  the seconds session request should complete with KErrNotReady 
  KErrNotReady is the default error timeout value set by the base state CState
  it is also be set the CCntRequest constructor
 
  @param  aError The error code with which the message will be completed if it times out 
  
 */ 
void CCntRequest::SetTimeOutError(TInt aError)
	{
	iTimeOutError = aError;
	}
	
/**
 Get the timeout error code	
 @return The timeout error code
*/ 
TInt CCntRequest::TimeOutError()const
	{
	return iTimeOutError;
	}

/** 
  Does not complete a RMessage - Internal Request do not contain a RMessage
  as they don't originate from the client.  Do nothing.
  @param  aErrorCode Not used
*/
void CReqInternal::Complete(TInt /* aErrorCode*/) 
	{
	}


// Request Helper classes
// -----------------------
// CCntRequestTimer
// CRequestStore

/** 
  CCntRequestTimer constructor
  CCntRequestTimer is an active object derived from CTimer
  @param aRequest The request which will timeout
  @param aReqStore The array that holds the deferred request    
  				   The request must be removed from the request store after 
  				   it is completed with an error code.
*/
CCntRequestTimer* CCntRequestTimer::NewL(CCntRequest& aRequest, CRequestStore& aReqStore)
	{
	CCntRequestTimer* self = new (ELeave) CCntRequestTimer(aRequest, aReqStore);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}
	
	
CCntRequestTimer::~CCntRequestTimer()
	{
	CTimer::Cancel();
	}
	
	
CCntRequestTimer::CCntRequestTimer(CCntRequest& aRequest, CRequestStore& aReqStore) 
				 : CTimer(CActive::EPriorityIdle), 
				 iRequest(aRequest), 
				 iReqStore(aReqStore) 
	{								  
	}


void CCntRequestTimer::ConstructL()
	{
	CTimer::ConstructL();
	CActiveScheduler::Add(this);
	}

void CCntRequestTimer::RunL()
	{
	// Complete the contact with the timeout error
	iRequest.Complete(iRequest.TimeOutError());
	CTimer::Cancel();
	iReqStore.RemoveD(&iRequest); 
	}

void CCntRequestTimer::Start()
	{ 
	CTimer::After(iRequest.TimeOut());
	}
		
void CCntRequestTimer::Stop()
	{
	CTimer::Cancel();
	}




/**
 Object are added when the session requests a Transaction to begin
 but the database is already in a transaction state for another session
 There can only ever handle one transaction request - no concurrency!

 @param aStateMachine The request store calls the statemachine to process the
 					  request.
*/ 
CRequestStore* CRequestStore::NewL(CCntStateMachine& aStateMachine)
	{
	CRequestStore* self = new (ELeave) CRequestStore(aStateMachine);
	return self;
	}

CRequestStore::CRequestStore(CCntStateMachine& aStateMachine): 
				iStateMachine(aStateMachine)
	{
	}

CRequestStore::~CRequestStore()
	{
	iStore.ResetAndDestroy();
	delete iActive;
	}

/**
 Remove the request from the store and delete the request
 
 @param aRequest The request that should be removed from the store and deleted
 				 The Request Store takes ownership of this object
*/
void CRequestStore::RemoveD(CCntRequest* aRequest)
	{
	TInt index = iStore.Find(aRequest);
	if (index > KErrNotFound)
		{
		iStore.Remove(index);	
		}
	delete aRequest;
	aRequest = NULL;
	}


/**
 Append a request to the request store

 @param aRequest  The request that is to be appended to the store
*/	
void CRequestStore::AppendL(CCntRequest* aRequest)
	{
	// The heap is safe since the request is appended to the contained store.
	// Pop as the stack will be out of sync when the statemachine processes 
	// the request.
	User::LeaveIfError(iStore.Append(aRequest));
	}

/** 
 Get the first request from the store 

 @return The request a the beginning of the stores array
 		 Ownership of the request is given to the calling method
*/
CCntRequest* CRequestStore::Request()
	{
	CCntRequest* request = iStore[KFirstRequest]; 
	iStore.Remove(KFirstRequest);
	
	return request;     // Return ownership
	}

/**
 Is there requests in the store
 
 @return ETrue if there are no request, EFalse otherwise
*/
TBool CRequestStore::IsEmpty()
	{
	if (iStore.Count() > 0)
		return EFalse;
	
	return ETrue;
	}

/**
  Activate the store.
  The request store uses the CActiveLoop for callbacks on each iteration 
  of the CActiveLoop only one request is processed until all
  requests have been processed.
 */
void CRequestStore::ActivateRequestsL()
	{
	if (!iActive)
		{
		iActive = CActiveLoop::NewL();
		}
	
	// Only process the requests in the store when it has been activated.
	// Don't process any requests added to the store after it has been activated.
	// Requests may be readded to the store & should not be processed twice 
	// in the same batch.
	// The store may already have been activated. 
	if ((!iActive->IsActive()) && (iNoOfRequests == 0))
		{
		iActive->Register(*this);	
		iNoOfRequests = iStore.Count();
		}
	}

/**
 Process the next request in the store.	
 If the request has timed out return an error
 otherwise process the request as normal.
 
 @ return ETrue if another step is required, EFalse if this is the last step
 */
TBool CRequestStore::DoStepL()
	{
	ASSERT(iCurrentRequest == NULL);

	iCurrentRequest = Request();
	iStateMachine.ProcessRequestL(iCurrentRequest); // ownership transferred
	
	// ProcessRequestL received ownership of the request, iCurrentRequest can be 
	// reset to NULL
	iCurrentRequest = NULL;
	
	--iNoOfRequests;

	if (iNoOfRequests == 0) 
		{
		return EFalse; // The store has tried to process all requests it holds	
		}

	return ETrue;
	}
	
/** 
 Error callback from the CActiveLoop class.	
 
 @ param aError The error to complete the current request (the request that is
 				being processed)
*/
void CRequestStore::DoError(TInt aError)
	{
	// there should always be a current request
	ASSERT(iCurrentRequest);
	if (iCurrentRequest)
		{
		iCurrentRequest->Complete(aError);
		delete iCurrentRequest;
		
		iCurrentRequest = NULL;
		}
	}


/** 
 CReqAsyncOpen constructor
 Request to open the database asyncronously 
 
 @see CCntRequest constructor   
*/
CReqAsyncOpen* CReqAsyncOpen::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqAsyncOpen* self = new (ELeave) CReqAsyncOpen(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CReqAsyncOpen::CReqAsyncOpen(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut )
: CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

void CReqAsyncOpen::ConstructL()
	{
	iMessage.ReadL(0,iFilename);
	}

/**
 Get the name of the file that is to be opened
 The DBManager may change this name. If no name has been passed by the client
 then the default is set by the manager
 
 @return The name of the file that should be opened
*/ 
TDes& CReqAsyncOpen::FileName()
	{
	return iFilename;
	}

TAccept CReqAsyncOpen::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}

/**
 CReqCloseTables constructor
 Close the database tables

 @see CCntRequest constructor    
*/ 
CReqCloseTables* CReqCloseTables::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
  	{
  	CReqCloseTables* self = new (ELeave) CReqCloseTables(aSessionId, aMessage, aTimeOut);
  	CleanupStack::PushL(self);
  	return self;
  	};
  
CReqCloseTables::CReqCloseTables(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
  :CCntRequest(aSessionId, aMessage, aTimeOut)
  	{
  	}
  
  
TAccept CReqCloseTables::VisitStateL(CState& aState)
  	{
  	return aState.AcceptRequestL(this);
  	}
  
  
/** 
  CReqReOpen 
  Re Open the database
  
  @see CCntRequest constructor    
*/
CReqReOpen* CReqReOpen::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqReOpen* self = new (ELeave) CReqReOpen(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}

CReqReOpen::CReqReOpen(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

TAccept CReqReOpen::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


/**
 CReqCancelAsyncOpen 
 Cancel an asyncronous open request
 
 @see CCntRequest constructor    
*/ 
CReqCancelAsyncOpen* CReqCancelAsyncOpen::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqCancelAsyncOpen* self = new (ELeave) CReqCancelAsyncOpen(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}

CReqCancelAsyncOpen::CReqCancelAsyncOpen(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}
	
TAccept CReqCancelAsyncOpen::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}



/**
 CReqUpdate 
 Parent Class for CReqUpdateCnt & CReqCommitCnt
 
 @see CCntRequest constructor   
*/
void CReqUpdate::ConstructL(CCntPackager& aPackager)
	{
	aPackager.SetBufferFromMessageL(iMessage);
	iCntItem = aPackager.UnpackCntItemLC();
	CleanupStack::Pop(iCntItem);
	}

CReqUpdate::~CReqUpdate()
	{
	delete iCntItem;
	}

CReqUpdate::CReqUpdate(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}



/** CReqUpdateCnt constructor
 Update a contact item - derived from CReqUpdate. Used in conjunction with CReqReadCnt. 
 Note: CReqCommitCnt is very similar to CReqUpdateCnt, but CReqCommitCnt is used with CReqOpenCnt
       and locks the contact. CReqUpdateCnt does not perform locking.
 
 @see CCntRequest constructor          
*/
CReqUpdateCnt* CReqUpdateCnt::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager)
	{
	CReqUpdateCnt* self = new (ELeave) CReqUpdateCnt(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	self->ConstructL(aPackager);
	return self;
	}

CReqUpdateCnt::CReqUpdateCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CReqUpdate(aSessionId, aMessage, aTimeOut)
	{
	}

TAccept CReqUpdateCnt::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}
	
	
/** 
 CReqSetSpeedDial constructor
 Set a speed dial key
 
 @see CCntRequest constructor   
*/ 
CReqSetSpeedDial* CReqSetSpeedDial::NewLC(	const TUint aSessionId, 
									const RMessage2& aMessage, 
									const TInt aTimeOut, 
									CContactItemViewDef& aItemViewDef,
									CCntServerSpeedDialManager& aSpeedDialManager,
									const CCntServerSpeedDialTable& aTable,
									MIniFileManager& aIniFileManager)
	{
	CReqSetSpeedDial* self = new (ELeave) CReqSetSpeedDial(	aSessionId, 
															aMessage, 
															aTimeOut, 
															aItemViewDef,
															aSpeedDialManager,
															aTable,
															aIniFileManager);
	CleanupStack::PushL(self);
	self->ConstructL();															
	return self;
	}

CReqSetSpeedDial::CReqSetSpeedDial(	const TUint aSessionId, 
									const RMessage2& aMessage, 
									const TInt aTimeOut, 
									CContactItemViewDef& aItemViewDef,
									CCntServerSpeedDialManager& aSpeedDialManager,
									const CCntServerSpeedDialTable& aTable,
									MIniFileManager& aIniFileManager)
:CCntRequest(aSessionId, aMessage, aTimeOut), iItemViewDef(&aItemViewDef), iSpeedDialManager(aSpeedDialManager), iTable(aTable), iIniFileManager(aIniFileManager)
	{
	}
	
void CReqSetSpeedDial::ConstructL()
	{
	// Read the message
	// Read position
	iSpeedDialIndex = iMessage.Int0();
	// Read contact id
	iContactId = static_cast<TContactItemId> (iMessage.Int1());
	// Read Field Index
	iTheFieldIndex = iMessage.Int2();	
	}

TAccept CReqSetSpeedDial::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}
	
TInt CReqSetSpeedDial::SpeedDialIndex()
	{
	return iSpeedDialIndex;
	}

TContactItemId CReqSetSpeedDial::TheContactId()
	{
	return iContactId;
	}

TInt CReqSetSpeedDial::TheFieldIndex()
	{
	return iTheFieldIndex;	
	}
		
const CContactItemViewDef& CReqSetSpeedDial::ItemViewDef()
	{
	return *iItemViewDef;
	}
	
CCntServerSpeedDialManager& CReqSetSpeedDial::SpeedDialManager()
	{
	return iSpeedDialManager;	
	}
	
const CCntServerSpeedDialTable& CReqSetSpeedDial::SpeedDialTable()
	{
	return iTable;
	}
	
MIniFileManager& CReqSetSpeedDial::IniFileManager()
	{
	return iIniFileManager;
	}

/**
 CReqSetOwnCard constructor
 Set the own card
 
 @see CCntRequest constructor    
*/ 
CReqSetOwnCard* CReqSetOwnCard::NewLC(	const TUint aSessionId, 
									const RMessage2& aMessage, 
									const TInt aTimeOut,
									CCntPackager& aPackager, 
									CContactItemViewDef& aItemViewDef,
									MIniFileManager& aIniFileManager)
	{
	CReqSetOwnCard* self = new (ELeave) CReqSetOwnCard(	aSessionId, 
														aMessage, 
														aTimeOut, 
														aItemViewDef,
														aIniFileManager);
	CleanupStack::PushL(self);
	self->ConstructL(aPackager);															
	return self;
	}

CReqSetOwnCard::CReqSetOwnCard(	const TUint aSessionId, 
									const RMessage2& aMessage, 
									const TInt aTimeOut, 
									CContactItemViewDef& aItemViewDef,
									MIniFileManager& aIniFileManager)
:CReqUpdate(aSessionId, aMessage, aTimeOut), iItemViewDef(&aItemViewDef), iIniFileManager(aIniFileManager)
	{
	}
	


TAccept CReqSetOwnCard::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}
	
TContactItemId CReqSetOwnCard::TheContactId()
	{
	return iContactId;
	}
		
const CContactItemViewDef& CReqSetOwnCard::ItemViewDef()
	{
	return *iItemViewDef;
	}
	
	
MIniFileManager& CReqSetOwnCard::IniFileManager()
	{
	return iIniFileManager;
	}
	
/** 
 CReqCommitCnt constructor
 Commit a contact item - used in conjunction with CReqOpenCnt
 Derived from CReqUpdate, and is very similar to CReqUpdateCnt but also locks the contact item
 so no other session can modify it.
 
 @see CCntRequest constructor   
*/ 
CReqCommitCnt* CReqCommitCnt::NewLC(const TUint aSessionId, 
									const RMessage2& aMessage, 
									const TInt aTimeOut, 
									CCntPackager& aPackager)
	{
	CReqCommitCnt* self = new (ELeave) CReqCommitCnt(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	self->ConstructL(aPackager);
	return self;
	}

CReqCommitCnt::CReqCommitCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CReqUpdate(aSessionId, aMessage, aTimeOut)
	{
	}

TAccept CReqCommitCnt::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}





/** CReqRead constructor   
 Parent class of CReqReadCnt & CReqOpenCnt
 CReqOpenCnt and CReqReadCnt are very similar 
 CReqOpenCnt results in the contact item being locked by the session, so no other session
 can modify the contact item.
 CReqReadCnt results in no locking.
 Note: The CRequest::CompleteL method is overridden in this class
 
 @see CCntRequest constructor    
*/ 
CReqRead::CReqRead(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager, CContactItemViewDef& aItemViewDef)
	:CCntRequest(aSessionId, aMessage, aTimeOut), 
	iPackager(aPackager), 
	iItemViewDef(&aItemViewDef),
	iViewDefCreated(EFalse)
	{
	}
	
void CReqRead::ConstructL()
	{
	if (iMessage.Int3())
		{
		iPackager.SetBufferFromMessageL(iMessage);
		iItemViewDef = iPackager.UnpackCntItemViewDefLC();
		iViewDefCreated = ETrue;
		iPackager.Clear();
		CleanupStack::Pop(iItemViewDef);
		}
	}

CReqRead::~CReqRead()
	{
	if (iViewDefCreated)	// Required - determine CntItemViewdef was extracted from iMessage
		{					// encapsulates ItemViewDef nicely. 
		delete iItemViewDef;
		}	
	}

const CContactItemViewDef& CReqRead::ItemViewDef()
	{
	return *iItemViewDef;
	}

/** Overridden CompleteL method 
 Completes the request item by packing the contact item into the RMessage slot
 and returning it to the client
 
 @param aCntItem The contact item that is returned to the client.
*/ 
void CReqRead::CompleteL(const CContactItem& aCntItem)
	{
	TInt size(0);
	TPtr8 cntItemBuff = iPackager.PackL(aCntItem, size); 
	// Write data if client buffer is large enough
	// otherwise return the required buffer size.
	if(iMessage.GetDesMaxLength(1) >= size)
		{ 
		iMessage.WriteL(1, cntItemBuff); 
		Complete(KErrNone);
		}
	else
		{ 
		Complete(size);	
		}
	}
	

/** CReqOpenCnt constructor
 Open a Contact Item - derived from CReqRead. 
 Very similar to CReqReadCnt but also results in the contact item being locked

 @see CReqRead constructor   
*/ 
CReqOpenCnt* CReqOpenCnt::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager, CContactItemViewDef& aItemViewDef)
	{
	CReqOpenCnt* self = new (ELeave) CReqOpenCnt(aSessionId, aMessage, aTimeOut, aPackager, aItemViewDef);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CReqOpenCnt::CReqOpenCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager, CContactItemViewDef& aItemViewDef)
	:CReqRead(aSessionId, aMessage, aTimeOut, aPackager, aItemViewDef) 
	{
	}

CReqOpenCnt::~CReqOpenCnt()
	{
	}


TAccept CReqOpenCnt::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


/**
 CReqReadCnt constructor
 Read a contact item - derived from CReqRead
 Very similar to CReqOpenCnt but does not result in locking the contact item

 @see CReqRead constructor   
 
*/ 
CReqReadCnt* CReqReadCnt::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager, CContactItemViewDef& aItemViewDef)
	{
	CReqReadCnt* self = new (ELeave) CReqReadCnt(aSessionId, aMessage, aTimeOut, aPackager, aItemViewDef);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CReqReadCnt::CReqReadCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager, CContactItemViewDef& aItemViewDef)
	:CReqRead(aSessionId, aMessage, aTimeOut, aPackager, aItemViewDef) 
	{
	}

CReqReadCnt::~CReqReadCnt()
	{
	}


TAccept CReqReadCnt::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}
	
/** 
 CReqDeleteCnt constructor
 Delete a contact Item
 
 @see CCntRequest constructor 
*/ 
CReqDeleteCnt* CReqDeleteCnt::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqDeleteCnt* self = new (ELeave) CReqDeleteCnt(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}

CReqDeleteCnt::CReqDeleteCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

CReqDeleteCnt::~CReqDeleteCnt()
	{
	}

TAccept CReqDeleteCnt::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}
	
	
/** CReqCloseCnt 
 Close a contact Item - Used in conjunction with CReqOpenCnt to remove the lock on the contact item 
 without committing.
 
 @see CCntRequest constructor
*/ 
CReqCloseCnt* CReqCloseCnt::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqCloseCnt* self = new (ELeave) CReqCloseCnt(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}

CReqCloseCnt::CReqCloseCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

CReqCloseCnt::~CReqCloseCnt()
	{
	}

TAccept CReqCloseCnt::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}	
	
/** 
  CReqCreateCnt constructor
  Create a contact item, this request is completed with the new contact item id or an error code.
  
  @see CCntRequest constructor
*/ 
CReqCreateCnt* CReqCreateCnt::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut, CCntPackager& aPackager)
	{
	CReqCreateCnt* self = new (ELeave) CReqCreateCnt(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	self->ConstructL(aPackager);
	return self;
	}

CReqCreateCnt::CReqCreateCnt(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
:CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

CReqCreateCnt::~CReqCreateCnt()
	{
	delete iCntItem;
	}

TAccept CReqCreateCnt::VisitStateL(CState& aState)
	{
	#if defined(__PROFILE_DEBUG__)
		RDebug::Print(_L("[CNTMODEL] MTD: CCntStateMachine::ProcessRequestL"));
	#endif 	
	
	return aState.AcceptRequestL(this);
	}

void CReqCreateCnt::ConstructL(CCntPackager& aPackager)
	{
	aPackager.SetBufferFromMessageL(iMessage);
	iCntItem = aPackager.UnpackCntItemLC();
	CleanupStack::Pop(iCntItem);
	}




///////////////// Database Transactin Request Class implemenations ////////////


/** 
 CReqDbBeginTrans constructor
 Request to begin a transaction - moves the state machine into a transaction state	
 
 @see CCntRequest constructor
*/
CReqDbBeginTrans* CReqDbBeginTrans::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqDbBeginTrans* self = new (ELeave) CReqDbBeginTrans(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}
	
TAccept CReqDbBeginTrans::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


	
CReqDbBeginTrans::CReqDbBeginTrans(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
				 :CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

/** 
 CReqDbCommitTrans constructor
 Request to Commit a transaction	
 Moves the database out of a transaction state and (usually) into a writable state
 
 @see CCntRequest constructor
 */
CReqDbCommitTrans* CReqDbCommitTrans::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqDbCommitTrans* self = new (ELeave) CReqDbCommitTrans(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;	
	}
	
TAccept CReqDbCommitTrans::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);	
	}

CReqDbCommitTrans::CReqDbCommitTrans(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)	
				  :CCntRequest(aSessionId, aMessage, aTimeOut)

	{
	}
	
	
/**
 CReqDbRollbackTrans constructor
 Request to Rollback a transaction - results in a database recovery 
 so it closes and re-opens the database if it has been damaged
 
 @see CCntRequest constructor 
*/ 
CReqDbRollbackTrans* CReqDbRollbackTrans::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqDbRollbackTrans* self = new (ELeave) CReqDbRollbackTrans(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;	
	}
	
TAccept CReqDbRollbackTrans::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);	
	}

CReqDbRollbackTrans::CReqDbRollbackTrans(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
				    :CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

	
/** 
 CReqBackupRestoreBegin constructor
 Notifys the state machine that a backup/restore is beginning

 @see CCntRequest constructor 
*/ 
CReqBackupRestoreBegin* CReqBackupRestoreBegin::NewLC()
	{
	CReqBackupRestoreBegin* self = new (ELeave) CReqBackupRestoreBegin();
	CleanupStack::PushL(self);
	return self;
	}

TAccept CReqBackupRestoreBegin::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}

/**
 CReqBackupRestoreEnd constructor
 Notifys the state machine that a restore has ended
 
 @see CCntRequest constructor 
 */
CReqBackupRestoreEnd* CReqBackupRestoreEnd::NewLC()
	{
	CReqBackupRestoreEnd* self = new (ELeave) CReqBackupRestoreEnd();
	CleanupStack::PushL(self);
	return self;
	}

TAccept CReqBackupRestoreEnd::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


/**
 CReqDiskSpaceLow constructor
 @see CCntRequest constructor 
 */
CReqDiskSpaceLow* CReqDiskSpaceLow::NewLC()
	{
	CReqDiskSpaceLow* self = new (ELeave) CReqDiskSpaceLow();
	CleanupStack::PushL(self);
	return self;
	}

TAccept CReqDiskSpaceLow::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


/** 
 CReqDiskSpaceNormal constructor
 @see CCntRequest constructor
*/
CReqDiskSpaceNormal* CReqDiskSpaceNormal::NewLC()
	{
	CReqDiskSpaceNormal* self = new (ELeave) CReqDiskSpaceNormal();
	CleanupStack::PushL(self);
	return self;
	}

TAccept CReqDiskSpaceNormal::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


/**
 CReqAsyncActivity constructor
 @see CCntRequest constructor 
 */
CReqAsyncActivity* CReqAsyncActivity::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqAsyncActivity* self = new (ELeave) CReqAsyncActivity(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}

CReqAsyncActivity::CReqAsyncActivity(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut )
: CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

TAccept CReqAsyncActivity::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}


/** 
 CReqNoAsyncActivity constructor
 @see CCntRequest constructor
*/
CReqNoAsyncActivity* CReqNoAsyncActivity::NewLC(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut)
	{
	CReqNoAsyncActivity* self = new (ELeave) CReqNoAsyncActivity(aSessionId, aMessage, aTimeOut);
	CleanupStack::PushL(self);
	return self;
	}

CReqNoAsyncActivity::CReqNoAsyncActivity(const TUint aSessionId, const RMessage2& aMessage, const TInt aTimeOut )
: CCntRequest(aSessionId, aMessage, aTimeOut)
	{
	}

TAccept CReqNoAsyncActivity::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);
	}

/**
  CReqInternalSessionUnlock constructor
  CReqInternalSessionUnlock unlocks all remaining locked contact items when
  a session closes. It is called from the session class destructor
  
  @see CCntReqInternal constructor  	
*/
CReqInternalSessionUnlock* CReqInternalSessionUnlock::NewLC(TInt aSessionId)
	{
	CReqInternalSessionUnlock* self = new (ELeave) CReqInternalSessionUnlock(aSessionId);
	CleanupStack::PushL(self);
	return self;		
	}
	
TAccept CReqInternalSessionUnlock::VisitStateL(CState& aState)
	{
	return aState.AcceptRequestL(this);		
	}

	
/////////////////////////CReqInternalAsyncOpen ////////////////////	
// Similar to CReqAsyncOpen but the request originates from inside the server.
CReqInternalAsyncOpen* CReqInternalAsyncOpen::NewLC(const TUint aSessionId, const TDesC& aFileName, TRequestStatus& aStatus)
	{
	CReqInternalAsyncOpen* self = new (ELeave) CReqInternalAsyncOpen(aSessionId, aFileName, aStatus);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}
	
CReqInternalAsyncOpen::CReqInternalAsyncOpen(const TUint aSessionId, const TDesC& aFileName, TRequestStatus& aStatus)
 : CReqAsyncOpen(aSessionId), iStatus(&aStatus)
	{
	iFilename = aFileName;
	}

void CReqInternalAsyncOpen::ConstructL()
	{
	*iStatus = KRequestPending;	
	}

void CReqInternalAsyncOpen::Complete(TInt aErrorCode)
	{
	User::RequestComplete(iStatus, aErrorCode);
	}

/////////////////////////CReqCancelInternalAsyncOpen ////////////////////	
// Similar to CReqCancelAsyncOpen but the request originates from inside the server,
// so there is no need to signal a RMessage that the request has completed
CReqCancelInternalAsyncOpen* CReqCancelInternalAsyncOpen::NewLC(const TUint aSessionId)
	{
	CReqCancelInternalAsyncOpen* self = new (ELeave) CReqCancelInternalAsyncOpen(aSessionId);
	CleanupStack::PushL(self);
	return self;	
	}
	
void CReqCancelInternalAsyncOpen::Complete(TInt /*errorCode*/)
	{
	}