os/persistentdata/persistentstorage/centralrepository/cenrepcli/clirep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "clirep.h"
sl@0
    17
#include <e32math.h>
sl@0
    18
#include "srvparams.h"
sl@0
    19
#include "srvreqs.h"
sl@0
    20
sl@0
    21
using namespace NCentralRepositoryConstants;
sl@0
    22
sl@0
    23
RRepositorySession* CClientRepository::Session()
sl@0
    24
	{
sl@0
    25
	return static_cast<RRepositorySession*>(Dll::Tls());
sl@0
    26
	}
sl@0
    27
	
sl@0
    28
CClientRepository* CClientRepository::NewLC(TUid aRepositoryUid)
sl@0
    29
	{
sl@0
    30
	CClientRepository* rep = new(ELeave) CClientRepository();
sl@0
    31
	CleanupStack::PushL(rep);
sl@0
    32
	rep->ConstructL(aRepositoryUid);
sl@0
    33
	return rep;
sl@0
    34
	}
sl@0
    35
sl@0
    36
void CClientRepository::ConstructL(TUid aRepositoryUid)
sl@0
    37
	{
sl@0
    38
	RRepositorySession* session = Session();
sl@0
    39
	
sl@0
    40
	if(session == NULL)
sl@0
    41
		{
sl@0
    42
		session = new (ELeave) RRepositorySession();
sl@0
    43
		CleanupStack::PushL(session);
sl@0
    44
		User::LeaveIfError(Dll::SetTls(session));
sl@0
    45
		CleanupStack::Pop(session);
sl@0
    46
		User::LeaveIfError(session->Connect());
sl@0
    47
		}
sl@0
    48
	else
sl@0
    49
		{
sl@0
    50
		session->IncrementSubSessionCounter();
sl@0
    51
		}
sl@0
    52
sl@0
    53
	iSubSession = new (ELeave) RRepositorySubSession();
sl@0
    54
	User::LeaveIfError(iSubSession->Open(session, EInitialise, TIpcArgs(aRepositoryUid.iUid)));
sl@0
    55
	}
sl@0
    56
sl@0
    57
CClientRepository::CClientRepository()
sl@0
    58
	{
sl@0
    59
	}
sl@0
    60
sl@0
    61
CClientRepository::~CClientRepository()
sl@0
    62
	{
sl@0
    63
	if(iSubSession)
sl@0
    64
		{
sl@0
    65
		iSubSession->Close();
sl@0
    66
		delete iSubSession;
sl@0
    67
		}
sl@0
    68
	RRepositorySession* session = Session();
sl@0
    69
	if(session && session->DecrementSubSessionCounter() == 0)
sl@0
    70
		{
sl@0
    71
		//The last subSesssion is closed. Time to close the session.
sl@0
    72
		session->Close();
sl@0
    73
		delete session;
sl@0
    74
		Dll::FreeTls();
sl@0
    75
		//SetSession(NULL);
sl@0
    76
		}
sl@0
    77
	}
sl@0
    78
sl@0
    79
TInt CClientRepository::Create(TUint32 aId, TInt aVal)
sl@0
    80
	{
sl@0
    81
	return iSubSession->SendReceive(ECreateInt, TIpcArgs(aId, aVal));
sl@0
    82
	}
sl@0
    83
sl@0
    84
TInt CClientRepository::Create(TUint32 aId, const TReal& aVal)
sl@0
    85
	{
sl@0
    86
	TPckg<TReal> p(aVal);
sl@0
    87
	return iSubSession->SendReceive(ECreateReal, TIpcArgs(aId, &p));
sl@0
    88
	}
sl@0
    89
	
sl@0
    90
TInt CClientRepository::Create(TUint32 aId, const TDesC8& aVal)
sl@0
    91
	{
sl@0
    92
	return iSubSession->SendReceive(ECreateString, TIpcArgs(aId, &aVal));
sl@0
    93
	}
sl@0
    94
sl@0
    95
TInt CClientRepository::Create(TUint32 aId, const TDesC16& aVal)
sl@0
    96
	{
sl@0
    97
	TPtrC8 ptr8((const TUint8*)aVal.Ptr(), aVal.Size());
sl@0
    98
	return iSubSession->SendReceive(ECreateString, TIpcArgs(aId, &ptr8));
sl@0
    99
	}
sl@0
   100
sl@0
   101
TInt CClientRepository::Delete(TUint32 aId)
sl@0
   102
	{
sl@0
   103
	return iSubSession->SendReceive(EDelete, TIpcArgs(aId));
sl@0
   104
	}
sl@0
   105
	
sl@0
   106
TInt CClientRepository::Delete(TUint32 aPartialKey, TUint32 aMask, TUint32 &aErrorKey)
sl@0
   107
	{
sl@0
   108
	aErrorKey = KUnspecifiedKey; // set in case not filled by server
sl@0
   109
	TPckg<TUint32> p(aErrorKey);
sl@0
   110
	return iSubSession->SendReceive(EDeleteRange, TIpcArgs(aPartialKey, aMask, &p));
sl@0
   111
	}
sl@0
   112
sl@0
   113
TInt CClientRepository::Get(TUint32 aId, TInt& aVal)
sl@0
   114
	{
sl@0
   115
	TPckg<TInt> p(aVal);
sl@0
   116
	return iSubSession->SendReceive(EGetInt, TIpcArgs(aId, &p));
sl@0
   117
	}
sl@0
   118
sl@0
   119
TInt CClientRepository::Set(TUint32 aId, TInt aVal)
sl@0
   120
	{
sl@0
   121
	return iSubSession->SendReceive(ESetInt, TIpcArgs(aId, aVal));
sl@0
   122
	}
sl@0
   123
sl@0
   124
TInt CClientRepository::Get(TUint32 aId, TReal& aVal)
sl@0
   125
	{
sl@0
   126
	TPckg<TReal> p(aVal);
sl@0
   127
	return iSubSession->SendReceive(EGetReal, TIpcArgs(aId, &p));
sl@0
   128
	}
sl@0
   129
sl@0
   130
TInt CClientRepository::Set(TUint32 aId, const TReal& aVal)
sl@0
   131
	{
sl@0
   132
	TPckg<TReal> p(aVal);
sl@0
   133
	return iSubSession->SendReceive(ESetReal, TIpcArgs(aId, &p));
sl@0
   134
	}
sl@0
   135
sl@0
   136
TInt CClientRepository::Get(TUint32 aId, TDes8& aVal)
sl@0
   137
	{
sl@0
   138
	TPckg<TInt> p(aVal.MaxLength());
sl@0
   139
	return iSubSession->SendReceive(EGetString, TIpcArgs(aId, &aVal, &p));
sl@0
   140
	}
sl@0
   141
sl@0
   142
TInt CClientRepository::Get(TUint32 aId, TDes8& aVal, TInt& aActualLen)
sl@0
   143
	{
sl@0
   144
	aActualLen = aVal.MaxLength();
sl@0
   145
	TPckg<TInt> p(aActualLen);
sl@0
   146
	return iSubSession->SendReceive(EGetString, TIpcArgs(aId, &aVal, &p));
sl@0
   147
	}
sl@0
   148
sl@0
   149
TInt CClientRepository::Set(TUint32 aId, const TDesC8& aVal)
sl@0
   150
	{
sl@0
   151
	return iSubSession->SendReceive(ESetString, TIpcArgs(aId, &aVal));
sl@0
   152
	}
sl@0
   153
sl@0
   154
TInt CClientRepository::Get(TUint32 aId, TDes& aVal)
sl@0
   155
	{
sl@0
   156
	TPtr8 ptr8((TUint8*)aVal.Ptr(), 0, aVal.MaxSize());
sl@0
   157
	
sl@0
   158
	TPckg<TInt> p(ptr8.MaxLength());
sl@0
   159
	
sl@0
   160
	TInt r = iSubSession->SendReceive(EGetString, TIpcArgs(aId, &ptr8, &p));
sl@0
   161
sl@0
   162
	if(r==KErrNone || r==KErrOverflow)
sl@0
   163
		{
sl@0
   164
		TInt len = ptr8.Length();
sl@0
   165
		// note the following handles the case where client is getting an odd-length 8-bit
sl@0
   166
		// descriptor into 16-bit aVal. Round up length and ensure the extra byte is zero.
sl@0
   167
		if(len&1)
sl@0
   168
			{
sl@0
   169
			ptr8.SetLength(len+1);   // set the length before trying to write the value			
sl@0
   170
			ptr8[len] = 0;
sl@0
   171
			}
sl@0
   172
		aVal.SetLength((len + 1)/2);
sl@0
   173
		}
sl@0
   174
sl@0
   175
	return r;
sl@0
   176
	}
sl@0
   177
	
sl@0
   178
TInt CClientRepository::Get(TUint32 aId, TDes& aVal, TInt& aActualLen)
sl@0
   179
	{
sl@0
   180
	TPtr8 ptr8((TUint8*)aVal.Ptr(), 0, aVal.MaxSize());
sl@0
   181
sl@0
   182
	aActualLen = ptr8.MaxLength();
sl@0
   183
	TPckg<TInt> p(aActualLen);
sl@0
   184
sl@0
   185
	TInt r = iSubSession->SendReceive(EGetString, TIpcArgs(aId, &ptr8, &p));
sl@0
   186
sl@0
   187
	if(r==KErrNone || r==KErrOverflow)
sl@0
   188
		{
sl@0
   189
		TInt len = ptr8.Length();
sl@0
   190
		// note the following handles the case where client is getting an odd-length 8-bit
sl@0
   191
		// descriptor into 16-bit aVal. Round up length and ensure the extra byte is zero.
sl@0
   192
		if(len&1)
sl@0
   193
			{
sl@0
   194
			ptr8.SetLength(len+1);   // set the length before trying to write the value			
sl@0
   195
			ptr8[len] = 0;
sl@0
   196
			}
sl@0
   197
		aVal.SetLength((len + 1)/2);
sl@0
   198
		aActualLen = ((aActualLen + 1)/2);
sl@0
   199
		}
sl@0
   200
sl@0
   201
	return r;
sl@0
   202
	}
sl@0
   203
sl@0
   204
TInt CClientRepository::Set(TUint32 aId, const TDesC& aVal)
sl@0
   205
	{
sl@0
   206
	TPtrC8 ptr8((const TUint8*)aVal.Ptr(), aVal.Size());
sl@0
   207
	return iSubSession->SendReceive(ESetString, TIpcArgs(aId, &ptr8));
sl@0
   208
	}
sl@0
   209
sl@0
   210
TInt CClientRepository::GetMeta(TUint32 aId, TUint32& aMeta)
sl@0
   211
	{
sl@0
   212
	TPckg<TUint32> p(aMeta);
sl@0
   213
	return iSubSession->SendReceive(EGetMeta, TIpcArgs(aId, &p));
sl@0
   214
	}
sl@0
   215
sl@0
   216
TInt CClientRepository::Move(TUint32 aSourcePartialId, TUint32 aTargetPartialId,
sl@0
   217
                             TUint32 aIdMask, TUint32 &aErrorId)
sl@0
   218
	{
sl@0
   219
	aErrorId = KUnspecifiedKey; // set in case not filled by server
sl@0
   220
	TPckg<TUint32> p(aErrorId);
sl@0
   221
	TKeyFilter srcKeyIdentifier = {aSourcePartialId, aIdMask};
sl@0
   222
	TKeyFilter tgtKeyIdentifier = {aTargetPartialId, aIdMask};
sl@0
   223
	TPckg<TKeyFilter> pSrc(srcKeyIdentifier);
sl@0
   224
	TPckg<TKeyFilter> pTrg(tgtKeyIdentifier);
sl@0
   225
	
sl@0
   226
	TInt r = iSubSession->SendReceive(EMove, TIpcArgs(&pSrc, &pTrg, &p));
sl@0
   227
	
sl@0
   228
	return r;
sl@0
   229
	}
sl@0
   230
	
sl@0
   231
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   232
//operations valid in transactions.
sl@0
   233
TInt CClientRepository::FindL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   234
	RArray<TUint32>& aFoundIds)
sl@0
   235
	{
sl@0
   236
    CleanupFailTransactionPushL();
sl@0
   237
	aFoundIds.Reset();	
sl@0
   238
	
sl@0
   239
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   240
	TUint32* start = uids.Begin();
sl@0
   241
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   242
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   243
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   244
sl@0
   245
	TInt r = iSubSession->SendReceive(EFind, TIpcArgs(&pIdentifier, 0, &ptr));
sl@0
   246
sl@0
   247
	if(r == KErrNone)
sl@0
   248
		{
sl@0
   249
		r = GetFindResult(uids, aFoundIds);
sl@0
   250
		if (r==KErrNoMemory)
sl@0
   251
			User::LeaveNoMemory();
sl@0
   252
		}
sl@0
   253
		
sl@0
   254
    CleanupStack::Pop();
sl@0
   255
	
sl@0
   256
	return r;
sl@0
   257
	}
sl@0
   258
sl@0
   259
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   260
//operations valid in transactions.
sl@0
   261
TInt CClientRepository::FindEqL(TUint32 aPartialId, TUint32 aIdMask, TInt aVal,
sl@0
   262
	RArray<TUint32>& aFoundIds)
sl@0
   263
	{
sl@0
   264
    CleanupFailTransactionPushL();
sl@0
   265
	aFoundIds.Reset();	
sl@0
   266
	
sl@0
   267
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   268
	TUint32* start = uids.Begin();
sl@0
   269
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   270
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   271
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   272
sl@0
   273
	TInt r = iSubSession->SendReceive(EFindEqInt, TIpcArgs(&pIdentifier, aVal, &ptr));
sl@0
   274
sl@0
   275
	if(r == KErrNone)
sl@0
   276
		{
sl@0
   277
		r = GetFindResult(uids, aFoundIds);
sl@0
   278
		if (r==KErrNoMemory)
sl@0
   279
			User::LeaveNoMemory();
sl@0
   280
		}
sl@0
   281
		
sl@0
   282
    CleanupStack::Pop();
sl@0
   283
	
sl@0
   284
	return r;
sl@0
   285
	}
sl@0
   286
sl@0
   287
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   288
//operations valid in transactions.
sl@0
   289
TInt CClientRepository::FindEqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   290
	const TReal& aVal, RArray<TUint32>& aFoundIds)
sl@0
   291
	{
sl@0
   292
    CleanupFailTransactionPushL();
sl@0
   293
	aFoundIds.Reset();	
sl@0
   294
	
sl@0
   295
	TPckg<TReal> pVal(aVal);
sl@0
   296
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   297
	TUint32* start = uids.Begin();
sl@0
   298
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   299
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   300
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   301
sl@0
   302
	TInt r = iSubSession->SendReceive(EFindEqReal, TIpcArgs(&pIdentifier, &pVal, &ptr));
sl@0
   303
sl@0
   304
	if(r == KErrNone)
sl@0
   305
		{
sl@0
   306
		r = GetFindResult(uids, aFoundIds);
sl@0
   307
		if (r==KErrNoMemory)
sl@0
   308
			User::LeaveNoMemory();
sl@0
   309
		}
sl@0
   310
		
sl@0
   311
    CleanupStack::Pop();
sl@0
   312
	
sl@0
   313
	return r;
sl@0
   314
	}
sl@0
   315
sl@0
   316
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   317
//operations valid in transactions.
sl@0
   318
TInt CClientRepository::FindEqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   319
	const TDesC8& aVal, RArray<TUint32>& aFoundIds)
sl@0
   320
	{
sl@0
   321
    CleanupFailTransactionPushL();
sl@0
   322
	aFoundIds.Reset();	
sl@0
   323
	
sl@0
   324
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   325
	TUint32* start = uids.Begin();
sl@0
   326
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   327
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   328
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   329
sl@0
   330
	TInt r = iSubSession->SendReceive(EFindEqString, TIpcArgs(&pIdentifier, &aVal, &ptr));
sl@0
   331
sl@0
   332
	if(r == KErrNone)
sl@0
   333
		{
sl@0
   334
		r = GetFindResult(uids, aFoundIds);
sl@0
   335
		if (r==KErrNoMemory)
sl@0
   336
			User::LeaveNoMemory();
sl@0
   337
		}
sl@0
   338
		
sl@0
   339
    CleanupStack::Pop();
sl@0
   340
	
sl@0
   341
	return r;
sl@0
   342
	}
sl@0
   343
sl@0
   344
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   345
//operations valid in transactions.
sl@0
   346
TInt CClientRepository::FindEqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   347
	const TDesC& aVal, RArray<TUint32>& aFoundIds)
sl@0
   348
	{
sl@0
   349
    CleanupFailTransactionPushL();
sl@0
   350
	aFoundIds.Reset();	
sl@0
   351
	
sl@0
   352
	TPtrC8 pVal((const TUint8*)aVal.Ptr(), aVal.Length()*2);
sl@0
   353
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   354
	TUint32* start = uids.Begin();
sl@0
   355
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   356
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   357
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   358
sl@0
   359
	TInt r = iSubSession->SendReceive(EFindEqString, TIpcArgs(&pIdentifier, &pVal, &ptr));
sl@0
   360
sl@0
   361
	if(r == KErrNone)
sl@0
   362
		{
sl@0
   363
		r = GetFindResult(uids, aFoundIds);
sl@0
   364
		if (r==KErrNoMemory)
sl@0
   365
			User::LeaveNoMemory();
sl@0
   366
		}
sl@0
   367
		
sl@0
   368
    CleanupStack::Pop();
sl@0
   369
	
sl@0
   370
	return r;
sl@0
   371
	}
sl@0
   372
sl@0
   373
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   374
//operations valid in transactions.
sl@0
   375
TInt CClientRepository::FindNeqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   376
	TInt aVal, RArray<TUint32>& aFoundIds)
sl@0
   377
	{
sl@0
   378
    CleanupFailTransactionPushL();
sl@0
   379
	aFoundIds.Reset();	
sl@0
   380
	
sl@0
   381
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   382
	TUint32* start = uids.Begin();
sl@0
   383
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   384
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   385
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   386
sl@0
   387
	TInt r = iSubSession->SendReceive(EFindNeqInt, TIpcArgs(&pIdentifier, aVal, &ptr));
sl@0
   388
sl@0
   389
	if(r == KErrNone)
sl@0
   390
		{
sl@0
   391
		r = GetFindResult(uids, aFoundIds);
sl@0
   392
		if (r==KErrNoMemory)
sl@0
   393
			User::LeaveNoMemory();
sl@0
   394
		}
sl@0
   395
		
sl@0
   396
    CleanupStack::Pop();
sl@0
   397
	
sl@0
   398
	return r;
sl@0
   399
	}
sl@0
   400
sl@0
   401
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   402
//operations valid in transactions.
sl@0
   403
TInt CClientRepository::FindNeqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   404
	const TReal& aVal, RArray<TUint32>& aFoundIds)
sl@0
   405
	{
sl@0
   406
    CleanupFailTransactionPushL();
sl@0
   407
	aFoundIds.Reset();	
sl@0
   408
	
sl@0
   409
	TPckg<TReal> pVal(aVal);
sl@0
   410
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   411
	TUint32* start = uids.Begin();
sl@0
   412
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   413
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   414
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   415
sl@0
   416
	TInt r = iSubSession->SendReceive(EFindNeqReal, TIpcArgs(&pIdentifier, &pVal, &ptr));
sl@0
   417
sl@0
   418
	if(r == KErrNone)
sl@0
   419
		{
sl@0
   420
		r = GetFindResult(uids, aFoundIds);
sl@0
   421
		if (r==KErrNoMemory)
sl@0
   422
			User::LeaveNoMemory();
sl@0
   423
		}
sl@0
   424
		
sl@0
   425
    CleanupStack::Pop();
sl@0
   426
	
sl@0
   427
	return r;
sl@0
   428
	}
sl@0
   429
sl@0
   430
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   431
//operations valid in transactions.
sl@0
   432
TInt CClientRepository::FindNeqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   433
	const TDesC8& aVal, RArray<TUint32>& aFoundIds)
sl@0
   434
	{
sl@0
   435
    CleanupFailTransactionPushL();
sl@0
   436
	aFoundIds.Reset();	
sl@0
   437
	
sl@0
   438
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   439
	TUint32* start = uids.Begin();
sl@0
   440
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   441
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   442
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   443
sl@0
   444
	TInt r = iSubSession->SendReceive(EFindNeqString, TIpcArgs(&pIdentifier, &aVal, &ptr));
sl@0
   445
sl@0
   446
	if(r == KErrNone)
sl@0
   447
		{
sl@0
   448
		r = GetFindResult(uids, aFoundIds);
sl@0
   449
		if (r==KErrNoMemory)
sl@0
   450
			User::LeaveNoMemory();
sl@0
   451
		}
sl@0
   452
		
sl@0
   453
    CleanupStack::Pop();
sl@0
   454
	
sl@0
   455
	return r;
sl@0
   456
	}
sl@0
   457
sl@0
   458
//Calls FailTransaction if it Leaves. This is the pattern for all client-side failure of
sl@0
   459
//operations valid in transactions.
sl@0
   460
TInt CClientRepository::FindNeqL(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   461
	const TDesC& aVal, RArray<TUint32>& aFoundIds)
sl@0
   462
	{
sl@0
   463
    CleanupFailTransactionPushL();
sl@0
   464
	aFoundIds.Reset();	
sl@0
   465
	
sl@0
   466
	TPtrC8 pVal((const TUint8*)aVal.Ptr(), aVal.Length()*2);
sl@0
   467
	TFixedArray<TUint32, KCentRepFindWithLenghtBufSize> uids;
sl@0
   468
	TUint32* start = uids.Begin();
sl@0
   469
	TPtr8 ptr(reinterpret_cast<TUint8*>(start), uids.Count() * uids.Length());
sl@0
   470
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   471
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   472
sl@0
   473
	TInt r = iSubSession->SendReceive(EFindNeqString, TIpcArgs(&pIdentifier, &pVal, &ptr));
sl@0
   474
sl@0
   475
	if(r == KErrNone)
sl@0
   476
		{
sl@0
   477
		r = GetFindResult(uids, aFoundIds);
sl@0
   478
		if (r==KErrNoMemory)
sl@0
   479
			User::LeaveNoMemory();
sl@0
   480
		}
sl@0
   481
		
sl@0
   482
    CleanupStack::Pop();
sl@0
   483
	
sl@0
   484
	return r;
sl@0
   485
	}
sl@0
   486
sl@0
   487
/** Private helper function for all the Find~L functions.
sl@0
   488
No need to call FailTransaction since all the methods that call this method calls
sl@0
   489
FailTransaction prior to this method.
sl@0
   490
@internalComponent
sl@0
   491
*/
sl@0
   492
TInt CClientRepository::GetFindResult(const TFixedArray<TUint32, KCentRepFindWithLenghtBufSize>& aUids, RArray<TUint32>& aFoundIds)
sl@0
   493
	{
sl@0
   494
	iClientErr = KErrNone;	
sl@0
   495
	const TUint32 numFound = aUids[0];
sl@0
   496
	const TUint32 numInitial = numFound > KCentRepFindBufSize ? KCentRepFindBufSize : numFound;
sl@0
   497
	const TUint32 numFinal = numFound > KCentRepFindBufSize ? numFound - KCentRepFindBufSize : 0;
sl@0
   498
	
sl@0
   499
	for(TUint32 i = 1; i <= numInitial; i++)
sl@0
   500
		{
sl@0
   501
		//initialise client error first
sl@0
   502
		iClientErr=aFoundIds.Append(aUids[i]);
sl@0
   503
		if (iClientErr!=KErrNone)
sl@0
   504
			return iClientErr;		
sl@0
   505
		}
sl@0
   506
	
sl@0
   507
	if(numFinal)
sl@0
   508
		{
sl@0
   509
		TAny* tempBuf = User::Alloc(numFinal * sizeof(TUint32));
sl@0
   510
		if (tempBuf==NULL)
sl@0
   511
			{
sl@0
   512
			return KErrNoMemory;
sl@0
   513
			}
sl@0
   514
		TPtr8 p(static_cast<TUint8*>(tempBuf), numFinal * sizeof(TUint32));
sl@0
   515
		TInt r = iSubSession->SendReceive(EGetFindResult, TIpcArgs(&p));
sl@0
   516
		if (r == KErrNone)
sl@0
   517
			{ 
sl@0
   518
			for(TUint32 i = 0; i < numFinal; i++)
sl@0
   519
				{
sl@0
   520
				iClientErr=aFoundIds.Append(static_cast<const TUint32*>(tempBuf)[i]);
sl@0
   521
				if (iClientErr!=KErrNone)
sl@0
   522
					{
sl@0
   523
					User::Free(tempBuf);
sl@0
   524
					return iClientErr;
sl@0
   525
					}
sl@0
   526
				}
sl@0
   527
			}
sl@0
   528
		User::Free(tempBuf);
sl@0
   529
		}		
sl@0
   530
	return iClientErr;
sl@0
   531
	}
sl@0
   532
sl@0
   533
TInt CClientRepository::NotifyRequest(TUint32 aId, TRequestStatus& aStatus)
sl@0
   534
	{
sl@0
   535
	TInt r = iSubSession->SendReceive(ENotifyRequestCheck, TIpcArgs(aId));
sl@0
   536
	if(r==KErrNone)
sl@0
   537
		iSubSession->SendReceive(ENotifyRequest, TIpcArgs(aId), aStatus);
sl@0
   538
	return r;
sl@0
   539
	}
sl@0
   540
sl@0
   541
TInt CClientRepository::NotifyCancel(TUint32 aId)
sl@0
   542
	{
sl@0
   543
	return iSubSession->SendReceive(ENotifyCancel, TIpcArgs(aId));
sl@0
   544
	}
sl@0
   545
sl@0
   546
TInt CClientRepository::NotifyCancelAll()
sl@0
   547
	{
sl@0
   548
	return iSubSession->SendReceive(ENotifyCancelAll);
sl@0
   549
	}
sl@0
   550
sl@0
   551
TInt CClientRepository::NotifyRequest(TUint32 aPartialId, TUint32 aIdMask,
sl@0
   552
	TRequestStatus& aStatus)
sl@0
   553
	{
sl@0
   554
	iSubSession->SendReceive(EGroupNotifyRequest,
sl@0
   555
		TIpcArgs(aPartialId, aIdMask), aStatus);
sl@0
   556
	return KErrNone;
sl@0
   557
	}
sl@0
   558
sl@0
   559
TInt CClientRepository::NotifyCancel(TUint32 aPartialId, TUint32 aIdMask)
sl@0
   560
	{
sl@0
   561
	TKeyFilter keyIdentifier = {aPartialId, aIdMask};
sl@0
   562
	TPckg<TKeyFilter> pIdentifier(keyIdentifier);
sl@0
   563
	
sl@0
   564
	return iSubSession->SendReceive(EGroupNotifyCancel, TIpcArgs(&pIdentifier));
sl@0
   565
	}
sl@0
   566
sl@0
   567
TInt CClientRepository::Reset()
sl@0
   568
	{
sl@0
   569
	return iSubSession->SendReceive(EResetAll);
sl@0
   570
	}
sl@0
   571
sl@0
   572
TInt CClientRepository::Reset(TUint32 aId)
sl@0
   573
	{
sl@0
   574
	return iSubSession->SendReceive(EReset, TIpcArgs(aId));
sl@0
   575
	}
sl@0
   576
sl@0
   577
TInt CClientRepository::StartTransaction(TTransactionMode aMode)
sl@0
   578
	{
sl@0
   579
	return iSubSession->SendReceive(ETransactionStart, TIpcArgs(aMode));
sl@0
   580
	}
sl@0
   581
sl@0
   582
void CClientRepository::StartTransaction(TTransactionMode aMode, TRequestStatus& aStatus)
sl@0
   583
	{
sl@0
   584
	iSubSession->SendReceive(ETransactionStart, TIpcArgs(aMode), aStatus);
sl@0
   585
	}
sl@0
   586
sl@0
   587
TInt CClientRepository::CommitTransaction(TUint32& aKeyInfo)
sl@0
   588
	{
sl@0
   589
	// set to KUnspecifiedKey in case failure happens before setting in server
sl@0
   590
	aKeyInfo = KUnspecifiedKey;
sl@0
   591
	TPckg<TUint32> p(aKeyInfo);
sl@0
   592
	return iSubSession->SendReceive(ETransactionCommit, TIpcArgs(&p));
sl@0
   593
	}
sl@0
   594
sl@0
   595
void CClientRepository::CommitTransaction(TDes8& aKeyInfo, TRequestStatus& aStatus)
sl@0
   596
	{
sl@0
   597
	// set to KUnspecifiedKey in case failure happens before setting in server
sl@0
   598
	aKeyInfo.Copy(TPckg<TUint32>(KUnspecifiedKey));
sl@0
   599
	iSubSession->SendReceive(ETransactionCommit, TIpcArgs(&aKeyInfo), aStatus);
sl@0
   600
	}
sl@0
   601
	
sl@0
   602
void CClientRepository::CancelTransaction()
sl@0
   603
	{
sl@0
   604
    iSubSession->SendReceive(ETransactionCancel);
sl@0
   605
	}
sl@0
   606
sl@0
   607
static void CancelTransactionCleanupOperation(TAny* aRepository)
sl@0
   608
    {
sl@0
   609
    static_cast<CClientRepository*>(aRepository)->CancelTransaction();
sl@0
   610
    }
sl@0
   611
sl@0
   612
// So CancelTransaction is called in case of Leave. Must pop with CleanupStack::Pop() or similar
sl@0
   613
void CClientRepository::CleanupCancelTransactionPushL()
sl@0
   614
    {
sl@0
   615
    CleanupStack::PushL(TCleanupItem(CancelTransactionCleanupOperation, this));
sl@0
   616
    }
sl@0
   617
    
sl@0
   618
void CClientRepository::FailTransaction()
sl@0
   619
	{
sl@0
   620
	if (iClientErr==KErrNone)
sl@0
   621
		iSubSession->SendReceive(ETransactionFail,TIpcArgs(KErrAbort));
sl@0
   622
	else
sl@0
   623
		iSubSession->SendReceive(ETransactionFail,TIpcArgs(iClientErr));
sl@0
   624
	//reset the internal client code
sl@0
   625
	iClientErr=KErrNone;
sl@0
   626
	}
sl@0
   627
	
sl@0
   628
// So FailTransaction is called in case of Leave. Must pop with CleanupStack::Pop() or similar
sl@0
   629
static void FailTransactionCleanupOperation(TAny* aRepository)
sl@0
   630
    {
sl@0
   631
    static_cast<CClientRepository*>(aRepository)->FailTransaction();
sl@0
   632
    }
sl@0
   633
	
sl@0
   634
void CClientRepository::CleanupFailTransactionPushL()
sl@0
   635
	{
sl@0
   636
	CleanupStack::PushL(TCleanupItem(FailTransactionCleanupOperation, this));
sl@0
   637
	}
sl@0
   638
sl@0
   639
TInt CClientRepository::TransactionState()
sl@0
   640
	{
sl@0
   641
	TInt iValue;
sl@0
   642
	
sl@0
   643
	TPckg<TInt> p(iValue);
sl@0
   644
	
sl@0
   645
	iSubSession->SendReceive(ETransactionState, TIpcArgs(&p));
sl@0
   646
	
sl@0
   647
	return iValue;
sl@0
   648
	}
sl@0
   649
sl@0
   650
TInt RRepositorySubSession::Open(RRepositorySession* aSession,TInt aFunction,const TIpcArgs& aArgs)
sl@0
   651
	{
sl@0
   652
	iSession = aSession;
sl@0
   653
	return(CreateSubSession(*aSession, aFunction, aArgs));
sl@0
   654
	}
sl@0
   655
	
sl@0
   656
void RRepositorySubSession::Close()
sl@0
   657
	{
sl@0
   658
	RSubSessionBase::CloseSubSession(EClose);
sl@0
   659
	}
sl@0
   660
sl@0
   661
TInt RRepositorySubSession::SendReceive(TInt aFunction) const
sl@0
   662
	{
sl@0
   663
	return RSubSessionBase::SendReceive(aFunction);
sl@0
   664
	}
sl@0
   665
	
sl@0
   666
TInt RRepositorySubSession::SendReceive(TInt aFunction, const TIpcArgs& aArgs) const
sl@0
   667
	{
sl@0
   668
	return RSubSessionBase::SendReceive(aFunction, aArgs);
sl@0
   669
	}
sl@0
   670
	
sl@0
   671
void RRepositorySubSession::SendReceive(TInt aFunction, const TIpcArgs& aArgs, TRequestStatus& aStatus) const
sl@0
   672
	{
sl@0
   673
	RSubSessionBase::SendReceive(aFunction, aArgs, aStatus);
sl@0
   674
	}
sl@0
   675
sl@0
   676
inline TInt RRepositorySession::IncrementSubSessionCounter()
sl@0
   677
	{
sl@0
   678
	return ++iSubSessionCounter;
sl@0
   679
	}
sl@0
   680
	
sl@0
   681
RRepositorySession ::RRepositorySession()
sl@0
   682
	:iSubSessionCounter(1)
sl@0
   683
	{
sl@0
   684
	}
sl@0
   685
	
sl@0
   686
inline TInt RRepositorySession::DecrementSubSessionCounter()
sl@0
   687
	{
sl@0
   688
	ASSERT(iSubSessionCounter > 0);
sl@0
   689
	return --iSubSessionCounter;
sl@0
   690
	}
sl@0
   691
sl@0
   692
#if defined(__CENTREP_SERVER_PERFTEST__) || defined(__CENTREP_SERVER_MEMTEST__) || defined(__CENTREP_SERVER_CACHETEST__)
sl@0
   693
TInt RRepositorySession::SendReceive(TInt aFunction) const
sl@0
   694
	{
sl@0
   695
	return RSessionBase::SendReceive(aFunction);
sl@0
   696
	}
sl@0
   697
sl@0
   698
TInt RRepositorySession::SendReceive(TInt aFunction,
sl@0
   699
	const TIpcArgs& aArgs) const
sl@0
   700
	{
sl@0
   701
	return RSessionBase::SendReceive(aFunction, aArgs);
sl@0
   702
	}
sl@0
   703
sl@0
   704
void RRepositorySession::SendReceive(TInt aFunction, const TIpcArgs& aArgs,
sl@0
   705
	TRequestStatus& aStatus) const
sl@0
   706
	{
sl@0
   707
	RSessionBase::SendReceive(aFunction, aArgs, aStatus);
sl@0
   708
	}
sl@0
   709
#endif
sl@0
   710
sl@0
   711
LOCAL_C TInt StartServer();
sl@0
   712
sl@0
   713
TInt RRepositorySession::Connect()
sl@0
   714
	{
sl@0
   715
   	const TVersion KVersion(KServerMajorVersion, KServerMinorVersion,
sl@0
   716
   		KServerBuildVersion);
sl@0
   717
   	TInt retry = 2;
sl@0
   718
   	TInt err = KErrGeneral;
sl@0
   719
   	// Use unlimited message slots as we can call subscribe multiple times per
sl@0
   720
   	// session.
sl@0
   721
   	TInt numMessageSlots = -1; 
sl@0
   722
   	for(;;)
sl@0
   723
   		{
sl@0
   724
   		// Try to create a new session with the server.
sl@0
   725
   		err = CreateSession(KServerName, KVersion, numMessageSlots);
sl@0
   726
   		if((err != KErrNotFound) && (err != KErrServerTerminated))
sl@0
   727
   			break; //completed
sl@0
   728
   		// Server not running, try to start it.
sl@0
   729
   		if(--retry==0)
sl@0
   730
			break; // Failed.
sl@0
   731
   		err = StartServer();
sl@0
   732
   		if((err != KErrNone) && (err != KErrAlreadyExists))
sl@0
   733
			break;	// Launched server
sl@0
   734
   		}
sl@0
   735
   	return err;
sl@0
   736
	}
sl@0
   737
sl@0
   738
//
sl@0
   739
// Start the server process or thread
sl@0
   740
//
sl@0
   741
LOCAL_C TInt StartServer()
sl@0
   742
	{
sl@0
   743
	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
sl@0
   744
	//
sl@0
   745
	// EPOC and EKA2 is easy, we just create a new server process. Simultaneous
sl@0
   746
	// launching of two such processes should be detected when the second one
sl@0
   747
	// attempts to create the server object, failing with KErrAlreadyExists.
sl@0
   748
	//
sl@0
   749
	RProcess server;
sl@0
   750
	TInt r=server.Create(KServerImg,KNullDesC,serverUid);
sl@0
   751
sl@0
   752
	if (r!=KErrNone)
sl@0
   753
		return r;
sl@0
   754
	TRequestStatus stat;
sl@0
   755
	server.Rendezvous(stat);
sl@0
   756
	if (stat!=KRequestPending)
sl@0
   757
		server.Kill(0);		// abort startup
sl@0
   758
	else
sl@0
   759
		server.Resume();	// logon OK - start the server
sl@0
   760
	User::WaitForRequest(stat);		// wait for start or death
sl@0
   761
	// we can't use the 'exit reason' if the server panicked as this
sl@0
   762
	// is the panic 'reason' and may be '0' which cannot be distinguished
sl@0
   763
	// from KErrNone
sl@0
   764
	r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
sl@0
   765
	server.Close();
sl@0
   766
	return r;
sl@0
   767
	}
sl@0
   768