messagingfw/msgsrvnstore/server/src/CMsvCachedStore.cpp
author hgs
Wed, 03 Nov 2010 22:41:46 +0530
changeset 62 db3f5fa34ec7
permissions -rw-r--r--
201044_02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     1
// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     2
// All rights reserved.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     4
// under the terms of "Eclipse Public License v1.0"
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     7
//
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     8
// Initial Contributors:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    10
//
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    11
// Contributors:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    12
//
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    13
// Description:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    14
//
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    15
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    16
#include "CMsvCachedStore.h"
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    17
#include "MSVSTORE.H"
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    18
#include "MSVUTILS.H"
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    19
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    20
#include <mmsvstoremanager.h>
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    21
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    22
const TInt KMsvFlatBufferChunkSize=0x400;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    23
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    24
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    25
enum TCMsvCachedStorePanic {
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    26
	EReadingWhileCommitingStream=1,
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    27
	EReadingWhileCommitingStore=2,
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    28
	EReadingOrWritingWhileRevertingStore=3,
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    29
	EReadingOrWritingWhileDeleteStream=4
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    30
	};
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    31
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    32
_LIT(KCachedStorePanic, "CMsvCachedStore");
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    33
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    34
void Panic(TCMsvCachedStorePanic aPanic)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    35
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    36
	User::Panic(KCachedStorePanic,aPanic);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    37
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    38
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    39
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    40
class HMsvReadBuf : public TMemBuf
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    41
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    42
public:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    43
	HMsvReadBuf(const TDesC8 &aBuf, CMsvCachedStore &aStore);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    44
private:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    45
	virtual void DoRelease();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    46
private:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    47
	CMsvCachedStore &iCachedStore;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    48
	};
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    49
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    50
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    51
class HMsvWriteBuf : public TBufBuf
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    52
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    53
public:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    54
	static HMsvWriteBuf* NewL(TUid aUid,CMsvCachedStore& aStore);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    55
private:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    56
	HMsvWriteBuf(TUid aUid,CMsvCachedStore& aStore);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    57
	virtual void DoRelease();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    58
	virtual void DoSynchL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    59
private:
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    60
	CBufBase*        iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    61
	CMsvCachedStore* iStore;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    62
	TUid			 iUid;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    63
	};
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    64
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    65
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    66
const TUid KMsvEntryFile={0x10003C68};
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    67
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    68
TPairedTUidHBufC8::TPairedTUidHBufC8() : iBuf(NULL) {}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    69
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    70
void TPairedTUidHBufC8::ExternalizeL(RWriteStream &aStream) const
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    71
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    72
	aStream << iUid;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    73
	aStream << *iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    74
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    75
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    76
void TPairedTUidHBufC8::InternalizeL(RReadStream &aStream)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    77
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    78
	aStream >> iUid;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    79
	// the 0x1000000 means that it will return KErrCorrupt
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    80
	// if the file says it contains a string of longer than 16 meg
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    81
	iBuf=HBufC8::NewL(aStream,0x1000000);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    82
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    83
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    84
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    85
CMsvCachedStore* CMsvCachedStore::OpenL(TMsvId aId, MMsvStoreManager& aStoreManager, TBool aReadOnly)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    86
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    87
	CMsvCachedStore* me = new(ELeave) CMsvCachedStore(aId, aStoreManager);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    88
	CleanupStack::PushL(me);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    89
	me->ConstructL(aReadOnly);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    90
	CleanupStack::Pop(me);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    91
	return(me);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    92
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    93
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    94
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    95
CMsvCachedStore::CMsvCachedStore(TMsvId aId, MMsvStoreManager& aStoreManager)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    96
	: iEntryId(aId), iStoreManager(aStoreManager)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    97
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    98
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
    99
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   100
void CMsvCachedStore::ConstructL(TBool aReadOnly)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   101
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   102
	iStreams=new(ELeave) CArrayFixFlat<TPairedTUidHBufC8>(5);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   103
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   104
	RFile storeFile;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   105
	TInt err = iStoreManager.OpenFileStoreForRead(iEntryId, storeFile);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   106
	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   107
	if( err != KErrNotFound )
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   108
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   109
		if( err==KErrNone )
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   110
			LoadL(storeFile); // the load takes ownership of the RFile
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   111
		else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   112
			User::Leave(err);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   113
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   114
	else if(aReadOnly)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   115
		User::Leave(err);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   116
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   117
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   118
TInt CMsvCachedStore::Size() const
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   119
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   120
	return iSize;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   121
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   122
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   123
void CMsvCachedStore::DeleteL()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   124
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   125
#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   126
	if(iHeaderFieldList.Count())
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   127
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   128
		iStoreManager.DeleteHeaderEntryL(iMtmId, iEntryId);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   129
		}	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   130
#endif
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   131
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   132
	iStoreManager.DeleteFileStoreL(iEntryId);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   133
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   134
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   135
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   136
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   137
#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   138
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   139
CMsvCachedStore* CMsvCachedStore::OpenL(TMsvId aId, MMsvStoreManager& aStoreManager, TBool aReadOnly,TUid aMtmId)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   140
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   141
	CMsvCachedStore* me = new(ELeave) CMsvCachedStore(aId, aStoreManager);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   142
	CleanupStack::PushL(me);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   143
	me->ConstructDBL(aReadOnly,aMtmId);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   144
	CleanupStack::Pop(me);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   145
	return(me);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   146
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   147
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   148
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   149
void CMsvCachedStore::ConstructDBL(TBool aReadOnly,TUid aMtmId)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   150
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   151
	iStreams=new(ELeave) CArrayFixFlat<TPairedTUidHBufC8>(5);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   152
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   153
	RFile storeFile;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   154
	TInt err = iStoreManager.OpenFileStoreForRead(iEntryId, storeFile);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   155
	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   156
	if(err != KErrNotFound)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   157
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   158
		if( err==KErrNone )
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   159
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   160
			LoadL(storeFile); // the load takes ownership of the RFile
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   161
			if(aReadOnly)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   162
				isNewEntry = EFalse;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   163
			else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   164
				isNewEntry = ETrue;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   165
			TRAP_IGNORE(LoadHeaderEntryL(aMtmId,aReadOnly));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   166
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   167
		else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   168
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   169
			User::Leave(err);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   170
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   171
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   172
	else 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   173
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   174
		TRAPD(err2, LoadHeaderEntryL(aMtmId,aReadOnly));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   175
		if (err2 == KErrNotFound)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   176
				{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   177
				User::Leave(err);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   178
				}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   179
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   180
	isDbStore=iStoreManager.DoesHeaderTableExist(aMtmId);	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   181
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   182
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   183
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   184
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   185
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   186
Assign the  header Fields in to header Field list.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   187
@param aHeaderFields : CHeaderFields*
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   188
@return None.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   189
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   190
void CMsvCachedStore::AssignL(CHeaderFields* aHeaderFields)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   191
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   192
	TUid uid = aHeaderFields->iUid;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   193
	for(TInt index=0; index<iHeaderFieldList.Count(); index++)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   194
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   195
		if(iHeaderFieldList[index]->iUid == uid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   196
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   197
			delete iHeaderFieldList[index];
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   198
			iHeaderFieldList[index] = aHeaderFields;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   199
			return;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   200
			}		
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   201
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   202
	iHeaderFieldList.AppendL(aHeaderFields);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   203
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   204
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   205
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   206
Get the header Fields for respective UID.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   207
@param aUid : A Uid
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   208
@param aHeaderFields : CHeaderFields*&
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   209
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   210
@return None.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   211
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   212
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   213
void CMsvCachedStore::GetHeaderL(TUid aUid, CHeaderFields*& aHeaderFields)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   214
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   215
	for(TInt index=0; index<iHeaderFieldList.Count(); index++)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   216
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   217
		if(iHeaderFieldList[index]->iUid == aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   218
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   219
			aHeaderFields = iHeaderFieldList[index];
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   220
			return;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   221
			}	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   222
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   223
	User::Leave(KErrNotFound);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   224
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   225
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   226
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   227
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   228
Load the Header Entry .
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   229
@param aMtmId A Mtm TUid.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   230
@return None.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   231
@internalComponent
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   232
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   233
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   234
void CMsvCachedStore::LoadHeaderEntryL(const TUid aMtmId,TBool aReadOnly)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   235
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   236
	TRAPD (err ,iStoreManager.LoadHeaderEntryL(aMtmId, iEntryId, iHeaderFieldList));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   237
	if((err == KErrNotFound) && aReadOnly)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   238
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   239
		User::Leave(err);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   240
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   241
		
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   242
	if(err == KErrNotFound)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   243
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   244
		isNewEntry = ETrue;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   245
		iSize = 0;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   246
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   247
	else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   248
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   249
		isNewEntry = EFalse;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   250
		if(err == KErrNone)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   251
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   252
			for(TInt index=0; index<iHeaderFieldList.Count(); index++)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   253
				{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   254
				for(TInt i =0 ; i < (iHeaderFieldList)[index]->iFieldPairList.Count() ;i++ )
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   255
					{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   256
					if(((iHeaderFieldList)[index]->iFieldPairList[i]->iFieldTextValue) != NULL)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   257
						iSize += (iHeaderFieldList)[index]->iFieldPairList[i]->iFieldTextValue->Length();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   258
					}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   259
				}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   260
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   261
		else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   262
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   263
			User::Leave(err);	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   264
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   265
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   266
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   267
	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   268
#endif
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   269
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   270
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   271
The CMsvCachedStore::Remove method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   272
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   273
Removes the stream from the array, ignores the fact that
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   274
it might not exist.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   275
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   276
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   277
@param				 aUid	Stream to remove
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   278
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   279
void CMsvCachedStore::Remove(TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   280
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   281
#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   282
	for(TInt index=0; index<iHeaderFieldList.Count(); index++)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   283
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   284
		if( (iHeaderFieldList)[index]->iUid == aUid )
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   285
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   286
			iHeaderFieldList.Remove(index);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   287
			TRAP_IGNORE(iStoreManager.UpdateHeaderEntryL(iMtmId, iEntryId, iHeaderFieldList)); 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   288
			return;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   289
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   290
		}	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   291
#endif
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   292
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   293
	DeleteStream(aUid);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   294
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   295
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   296
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   297
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   298
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   299
The CMsvCachedStore::RemoveL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   300
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   301
Removes a stream from the array, leaves if the stream is not found
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   302
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   303
@leave 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   304
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   305
@param				 aUid	Stream to remove
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   306
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   307
void CMsvCachedStore::RemoveL(TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   308
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   309
	User::LeaveIfError(DeleteStream(aUid));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   310
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   311
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   312
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   313
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   314
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   315
The CMsvCachedStore::Revert method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   316
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   317
Reloads the array of streams from the store
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   318
ignores errors that might occur.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   319
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   320
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   321
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   322
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   323
void CMsvCachedStore::Revert()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   324
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   325
     TRAP_IGNORE(RevertL());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   326
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   327
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   328
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   329
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   330
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   331
The CMsvCachedStore::RevertL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   332
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   333
  Reloads the streams from the file
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   334
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   335
@leave
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   336
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   337
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   338
void CMsvCachedStore::RevertL()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   339
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   340
	__ASSERT_ALWAYS(iReaderCount==0 && iWriterCount==0, Panic(EReadingOrWritingWhileRevertingStore));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   341
	CleanArray();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   342
	iStreams=new(ELeave) CArrayFixFlat<TPairedTUidHBufC8>(5);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   343
	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   344
	RFile storeFile;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   345
	User::LeaveIfError(iStoreManager.OpenFileStoreForRead(iEntryId, storeFile));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   346
	LoadL(storeFile); // the load takes ownership of the RFile	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   347
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   348
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   349
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   350
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   351
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   352
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   353
The CMsvCachedStore::Commit method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   354
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   355
  Saves the current state of the stream array to the file
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   356
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   357
@return			error
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   358
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   359
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   360
TInt CMsvCachedStore::Commit() 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   361
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   362
	TRAPD(error,CommitL());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   363
	return(error);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   364
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   365
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   366
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   367
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   368
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   369
The CMsvCachedStore::CommitL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   370
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   371
  Saves the current state of the stream array to the file
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   372
  leaves with if error occurs
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   373
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   374
@leave
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   375
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   376
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   377
void CMsvCachedStore::CommitL()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   378
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   379
	__ASSERT_ALWAYS(iWriterCount==0,Panic(EReadingWhileCommitingStore));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   380
	SaveL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   381
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   382
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   383
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   384
void CMsvCachedStore::CleanArray()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   385
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   386
	if(iStreams!=NULL)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   387
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   388
		TInt count=iStreams->Count();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   389
		while(count--) delete iStreams->At(count).iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   390
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   391
	delete iStreams;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   392
	iStreams=NULL;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   393
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   394
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   395
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   396
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   397
The CMsvCachedStore::~CMsvCachedStore method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   398
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   399
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   400
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   401
CMsvCachedStore::~CMsvCachedStore()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   402
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   403
#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   404
	iHeaderFieldList.ResetAndDestroy();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   405
	iHeaderFieldList.Close();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   406
#endif
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   407
	CleanArray();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   408
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   409
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   410
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   411
TBool CMsvCachedStore::IsNullL() const
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   412
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   413
#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   414
	if(iStreams->Count()==0)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   415
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   416
		if(iHeaderFieldList.Count() ==0)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   417
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   418
			return ETrue;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   419
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   420
		else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   421
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   422
			return EFalse;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   423
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   424
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   425
	else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   426
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   427
		return EFalse;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   428
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   429
#else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   430
	return(iStreams->Count()==0);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   431
#endif
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   432
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   433
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   434
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   435
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   436
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   437
The CMsvCachedStore::IsPresentL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   438
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   439
  checks to see if the stream is in the array
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   440
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   441
@return		ETrue if it exists EFalse if not.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   442
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   443
@param				 aUid	stream to check for
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   444
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   445
TBool CMsvCachedStore::IsPresentL(TUid aUid) const
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   446
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   447
#if (defined SYMBIAN_MESSAGESTORE_HEADER_BODY_USING_SQLDB)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   448
	for(TInt index=0; index<iHeaderFieldList.Count(); index++)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   449
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   450
		if(iHeaderFieldList[index]->iUid == aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   451
			{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   452
			return ETrue;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   453
			}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   454
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   455
#endif 	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   456
	TInt pos;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   457
	if(FindStream(aUid,pos)==KErrNone) return(ETrue);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   458
	else return(EFalse);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   459
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   460
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   461
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   462
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   463
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   464
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   465
The CMsvCachedStore::CreateOrReplaceStreamL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   466
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   467
  If a stream is present with this id it replaces the
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   468
  data in that stream with aBuf, otherwise it adds a new
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   469
  stream with the data in aBuf
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   470
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   471
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   472
@param				 aUid	stream id
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   473
@param				 *aBuf	data, takes ownership
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   474
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   475
void CMsvCachedStore::CreateOrReplaceStreamL(TUid aUid, CBufBase &aBuf)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   476
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   477
	__ASSERT_ALWAYS(iReaderCount==0, Panic(EReadingWhileCommitingStream));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   478
	TInt pos;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   479
	HBufC8* buffer=FlattenLC(aBuf);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   480
	if(FindStream(aUid,pos)==KErrNone)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   481
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   482
		delete iStreams->At(pos).iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   483
		iStreams->At(pos).iBuf=buffer;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   484
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   485
	else
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   486
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   487
		TPairedTUidHBufC8 entry;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   488
		entry.iUid=aUid;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   489
		entry.iBuf=buffer;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   490
		iStreams->InsertL(pos,entry);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   491
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   492
	CleanupStack::Pop(buffer);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   493
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   494
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   495
HBufC8* CMsvCachedStore::FlattenLC(CBufBase &aBuf)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   496
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   497
	TInt size=aBuf.Size();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   498
	HBufC8 *buffer=HBufC8::NewLC(size);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   499
	TPtr8 ptr(buffer->Des());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   500
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   501
	RDesWriteStream writer(ptr);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   502
	RBufReadStream reader(aBuf);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   503
	writer.WriteL(reader,size);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   504
	writer.CommitL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   505
	return(buffer);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   506
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   507
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   508
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   509
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   510
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   511
The CMsvCachedStore::GetStream method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   512
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   513
  If it returns with KErrNone it sets aData to point to the
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   514
  data in the store this id
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   515
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   516
@return			error code if the stream was not found
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   517
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   518
@param				 aUid	stream id
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   519
@param				 *&aData	returned pointer to data
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   520
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   521
HBufC8* CMsvCachedStore::GetStreamL(TUid aUid) const
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   522
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   523
	TInt pos;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   524
	if(FindStream(aUid, pos) != 0)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   525
		User::Leave(KErrNotFound);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   526
	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   527
	return iStreams->At(pos).iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   528
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   529
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   530
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   531
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   532
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   533
The CMsvCachedStore::FindStream method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   534
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   535
  Finds the stream of id aUid, if it returns with KErrNone then
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   536
  pos is set to the position of the 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   537
  if it is not found pos is set to where is should be inserted
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   538
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   539
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   540
@param				 aUid
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   541
@param				 &pos
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   542
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   543
TInt CMsvCachedStore::FindStream(TUid aUid,TInt &pos) const
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   544
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   545
	TPairedTUidHBufC8 entry;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   546
	entry.iUid=aUid;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   547
	TKeyArrayFix key(_FOFF(TPairedTUidHBufC8,iUid),ECmpTInt32);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   548
	return(iStreams->FindIsq(entry,key,pos));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   549
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   550
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   551
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   552
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   553
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   554
The CMsvCachedStore::DeleteStream method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   555
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   556
  deletes the stream with this id
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   557
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   558
@return			error code KErrNone unless the stream wasn't found
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   559
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   560
@param				 aUid	the stream id to delete
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   561
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   562
TInt CMsvCachedStore::DeleteStream(TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   563
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   564
	__ASSERT_ALWAYS(iReaderCount==0 && iWriterCount==0,Panic(EReadingOrWritingWhileDeleteStream));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   565
	TInt pos;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   566
	TInt error=FindStream(aUid,pos);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   567
	if(error==KErrNone)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   568
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   569
		delete iStreams->At(pos).iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   570
		iStreams->Delete(pos);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   571
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   572
	return(error);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   573
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   574
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   575
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   576
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   577
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   578
The CMsvCachedStore::LoadL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   579
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   580
loads the streams from a file
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   581
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   582
  should this create the file if it is corupt???
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   583
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   584
@leave				leaves if we can't find the file, if we run out of memory, if the file is corrupt
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   585
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   586
@param				 &aFile	file to load the streams from, take ownership of the file
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   587
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   588
void CMsvCachedStore::LoadL(RFile &aFile)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   589
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   590
	__ASSERT_DEBUG(iStreams->Count()==0,User::Invariant());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   591
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   592
	// Need to cache the file size
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   593
	User::LeaveIfError(aFile.Size(iSize));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   594
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   595
	RFileReadStream in(aFile);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   596
	in.PushL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   597
	TCheckedUid check;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   598
	in >> check;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   599
	if(check.UidType().IsValid()==EFalse) User::Leave(KErrCorrupt);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   600
	if(check.UidType()[0]!=KMsvEntryFile) User::Leave(KErrCorrupt);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   601
	in >> *iStreams;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   602
	CleanupStack::PopAndDestroy(); // close RFileReadStream in
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   603
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   604
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   605
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   606
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   607
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   608
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   609
The CMsvCachedStore::SaveL method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   610
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   611
Saves the current state to iFileName
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   612
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   613
@leave				leaves if we can't replace the file, if we run out of disk space,
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   614
@internalAll
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   615
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   616
void CMsvCachedStore::SaveL()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   617
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   618
	RFile tempStoreFile;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   619
	iStoreManager.OpenTempStoreFileL(iEntryId, tempStoreFile);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   620
	RFileWriteStream out(tempStoreFile);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   621
	out.PushL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   622
	WriteToFileStreamL(out);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   623
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   624
	//get the size of the stream, in previous call the Stream is already commited to the file, the next step simply replace the file with this temp new file so size should be the same
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   625
	//the SizeL in this case will not involve any file i/o calls.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   626
	iSize=out.Sink()->SizeL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   627
	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   628
    CleanupStack::PopAndDestroy(&out); // close RFileWriteStream out
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   629
    iStoreManager.ReplaceFileStoreL(iEntryId);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   630
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   631
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   632
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   633
void CMsvCachedStore::WriteToFileStreamL(RFileWriteStream &aOut)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   634
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   635
	TCheckedUid check(TUidType(KMsvEntryFile,KMsvEntryFile,KNullUid));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   636
	aOut << check;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   637
	aOut << *iStreams;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   638
	aOut.CommitL();	
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   639
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   640
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   641
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   642
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   643
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   644
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   645
EXPORT_C void RMsvReadStream::OpenL(const CMsvStore& aMsvStore, TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   646
/** Prepares an existing stream with UID for reading.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   647
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   648
After this function has been called, the stream can be read from with the 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   649
functions provided by the RReadStream base class.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   650
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   651
@param aMsvStore The CMsvStore the stream is in. 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   652
@param aUid The UID of the stream to open with read access 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   653
@leave KErrNotFound There is no stream with UID aUid 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   654
@leave Other Standard stream leave codes. */
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   655
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   656
	HBufC8 *buffer=aMsvStore.iStore->GetStreamL(aUid);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   657
	HMsvReadBuf *tidy= new (ELeave) HMsvReadBuf(*buffer, *(aMsvStore.iStore));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   658
	Attach(tidy);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   659
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   660
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   661
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   662
EXPORT_C void RMsvReadStream::OpenLC(const CMsvStore& aMsvStore,TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   663
/** Prepares an existing stream with UID for reading.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   664
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   665
After this function has been called, the stream can be read from with the 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   666
functions provided by the RReadStream base class.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   667
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   668
The object is placed on the cleanup stack. 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   669
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   670
@param aMsvStore The CMsvStore the stream is in. 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   671
@param aUid The UID of the stream to open with read access 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   672
@leave KErrNotFound There is no stream with UID aUid 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   673
@leave Other Standard stream leave codes. */
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   674
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   675
	OpenL(aMsvStore,aUid);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   676
	PushL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   677
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   678
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   679
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   680
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   681
/**
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   682
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   683
The RMsvReadStream::OpenLC method
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   684
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   685
ties the read stream to a HMsvReadBuf
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   686
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   687
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   688
@leave				leaves if we can't find the stream
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   689
@param				 aStore   aStore is the CMsvCachedStore within which we look for the stream
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   690
@param				 aUid		id of stream to open for reading
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   691
*/
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   692
void RMsvReadStream::OpenLC(CMsvCachedStore& aStore,TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   693
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   694
	HBufC8 *buffer=aStore.GetStreamL(aUid);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   695
	HMsvReadBuf *tidy= new (ELeave) HMsvReadBuf(*buffer, aStore);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   696
	Attach(tidy);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   697
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   698
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   699
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   700
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   701
HMsvWriteBuf* HMsvWriteBuf::NewL(TUid aUid,CMsvCachedStore& aStore)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   702
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   703
	HMsvWriteBuf* self=new (ELeave) HMsvWriteBuf(aUid,aStore);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   704
	self->PushL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   705
	self->iBuf=CBufSeg::NewL(KMsvFlatBufferChunkSize);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   706
	CleanupStack::Pop(self);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   707
	self->Set(*(self->iBuf),0);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   708
	return(self);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   709
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   710
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   711
HMsvWriteBuf::HMsvWriteBuf(TUid aUid,CMsvCachedStore& aStore) : iBuf(NULL), iStore(&aStore), iUid(aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   712
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   713
	iStore->iWriterCount++;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   714
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   715
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   716
void HMsvWriteBuf::DoRelease()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   717
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   718
	__ASSERT_DEBUG(iStore==NULL || iStore->iWriterCount>0,User::Invariant());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   719
	delete iBuf;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   720
	// we decrememt the writer count if it wasn't done in DoSynchL (called if client commits)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   721
	if(iStore) iStore->iWriterCount--;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   722
	iStore=NULL;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   723
	delete this;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   724
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   725
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   726
void HMsvWriteBuf::DoSynchL()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   727
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   728
	// if we have already sync'd it will be null so ignore the sync
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   729
	if(iStore!=NULL)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   730
		{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   731
		__ASSERT_DEBUG(iStore->iWriterCount>0,User::Invariant());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   732
		TBufBuf::DoSynchL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   733
		iStore->CreateOrReplaceStreamL(iUid,*iBuf);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   734
		// decrement the writer count, after a commit all writes are ignored, this is the
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   735
		// same as other stores, also set iStore to NULL so we don't decrement the writer
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   736
		// count in DoRelease as well.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   737
		iStore->iWriterCount--;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   738
		iStore=NULL;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   739
		}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   740
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   741
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   742
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   743
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   744
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   745
HMsvReadBuf::HMsvReadBuf(const TDesC8 &aBuf,CMsvCachedStore &aStore) : iCachedStore(aStore)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   746
	{ 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   747
	TUint8* ptr=(TUint8 *)aBuf.Ptr();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   748
	Set(ptr,ptr+aBuf.Length(),ERead);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   749
	aStore.iReaderCount++;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   750
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   751
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   752
void HMsvReadBuf::DoRelease()
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   753
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   754
	__ASSERT_ALWAYS(iCachedStore.iReaderCount>0,User::Invariant());
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   755
	iCachedStore.iReaderCount--;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   756
	delete this;
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   757
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   758
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   759
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   760
EXPORT_C void RMsvWriteStream::AssignLC(CMsvStore &aMsvStore, TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   761
/** Prepares a stream for writing; the object is placed on the cleanup stack. 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   762
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   763
After this function has been called, the stream can be written to with the 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   764
functions provided by the RWriteStream base class.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   765
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   766
If the stream does not exist, it is created.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   767
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   768
@param aMsvStore The CMsvStore the stream is in 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   769
@param aUid The UID of the stream to open with write access 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   770
@leave KErrAccessDenied Store is not open for writing 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   771
@leave Other Standard stream leave codes */
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   772
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   773
	if (aMsvStore.iLockStatus==CMsvStore::EMsvStoreUnlocked)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   774
		User::Leave(KErrAccessDenied);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   775
	AssignL(aMsvStore,aUid);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   776
	PushL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   777
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   778
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   779
EXPORT_C void RMsvWriteStream::AssignL(CMsvStore &aMsvStore, TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   780
/** Prepares a stream for writing. 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   781
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   782
After this function has been called, the stream can be written to with the 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   783
functions provided by the RWriteStream base class.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   784
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   785
If the stream does not exist, it is created.
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   786
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   787
@param aMsvStore The CMsvStore the stream is in 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   788
@param aUid The UID of the stream to open with write access 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   789
@leave KErrAccessDenied Store is not open for writing 
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   790
@leave Other Standard stream leave codes */
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   791
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   792
	if (aMsvStore.iLockStatus==CMsvStore::EMsvStoreUnlocked)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   793
		User::Leave(KErrAccessDenied);
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   794
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   795
	Attach(HMsvWriteBuf::NewL(aUid,*(aMsvStore.iStore)));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   796
	}
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   797
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   798
void RMsvWriteStream::AssignLC(CMsvCachedStore &aStore, TUid aUid)
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   799
	{
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   800
	Attach(HMsvWriteBuf::NewL(aUid,aStore));
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   801
	PushL();
db3f5fa34ec7 201044_02
hgs
parents:
diff changeset
   802
	}