notepad/notepad1/SyncMLAdapter/src/NSmlNotepadDataStore.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  DS notepad data store
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <barsc.h>
       
    21 #include <bautils.h>
       
    22 #include <s32mem.h>
       
    23 #include <sysutil.h>
       
    24 #include <nsmlnotepaddatastoreformat.rsg>
       
    25 #include <data_caging_path_literals.hrh>
       
    26 #include <SmlDataProvider.h>
       
    27 #include <txtetext.h>
       
    28 #include <charconv.h>
       
    29 #include <nsmlchangefinder.h>
       
    30 
       
    31 #include "NSmlNotepadDatastore.h"
       
    32 #include "NSmlNotepadModsFetcher.h"
       
    33 #include "nsmlnotepaddefs.h"
       
    34 #include "nsmlnotepadDatabase.h"
       
    35 
       
    36 #include "nsmlnotepadlogging.h"
       
    37 	
       
    38 // ----------------------------------------------------------------------------
       
    39 // CNSmlNotepadDataStore::NewL
       
    40 // ----------------------------------------------------------------------------
       
    41 CNSmlNotepadDataStore* CNSmlNotepadDataStore::NewL()
       
    42 	{
       
    43 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::NewL: BEGIN");
       
    44 	CNSmlNotepadDataStore* self = new ( ELeave ) CNSmlNotepadDataStore();
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL();
       
    47 	CleanupStack::Pop(self); // self
       
    48 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::NewL: END");
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // CNSmlNotepadDataStore::~CNSmlNotepadDataStore()
       
    54 // ----------------------------------------------------------------------------
       
    55 
       
    56 CNSmlNotepadDataStore::~CNSmlNotepadDataStore()
       
    57 	{
       
    58 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::~CNSmlNotepadDataStore(): begin");
       
    59 	if(iOwnStoreFormat)
       
    60 	{
       
    61 		delete iOwnStoreFormat;
       
    62 		iOwnStoreFormat = NULL;
       
    63 	}
       
    64 	if(iDefaultStoreName)
       
    65 	{
       
    66 		delete iDefaultStoreName;
       
    67 		iDefaultStoreName = NULL;
       
    68 	}
       
    69 	if(iNpdDb)
       
    70 	{
       
    71 		delete iNpdDb;
       
    72 		iNpdDb = NULL;
       
    73 	}
       
    74 	if(iReplacedUids)
       
    75 	{
       
    76 		delete iReplacedUids;
       
    77 		iReplacedUids = NULL;
       
    78 	}
       
    79 	if(iMovedUids)
       
    80 	{
       
    81 		delete iMovedUids;
       
    82 		iMovedUids = NULL;
       
    83 	}
       
    84 	if(iSoftDeletedUids)
       
    85 	{
       
    86 		delete iSoftDeletedUids;
       
    87 		iSoftDeletedUids = NULL;
       
    88 	}
       
    89 	if(iDeletedUids)
       
    90 	{
       
    91 		delete iDeletedUids;
       
    92 		iDeletedUids = NULL;
       
    93 	}
       
    94 	if(iNewUids)
       
    95 	{
       
    96 		delete iNewUids;
       
    97 		iNewUids = NULL;
       
    98 	}
       
    99 	iRfs.Close();
       
   100 	iStringPool.Close();
       
   101 	if(iOpenedStoreName)
       
   102 	{
       
   103 		delete iOpenedStoreName;
       
   104 		iOpenedStoreName = NULL;
       
   105 	}
       
   106 	if(iItemData)
       
   107 	{
       
   108 		delete iItemData;
       
   109 		iItemData = NULL;	
       
   110 	}
       
   111 	if ( iChangeFinder )
       
   112 		{
       
   113 		TRAP_IGNORE( iChangeFinder->CloseL() );
       
   114 		delete iChangeFinder;
       
   115 		iChangeFinder = NULL;
       
   116 		}
       
   117 	if ( iNotepadModsFetcher )
       
   118 	{
       
   119 	iNotepadModsFetcher->Cancel(); 
       
   120 	delete iNotepadModsFetcher;
       
   121 	iNotepadModsFetcher = NULL;
       
   122 	}
       
   123 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::~CNSmlNotepadDataStore(): end");		
       
   124 	}
       
   125 
       
   126 
       
   127 // ----------------------------------------------------------------------------
       
   128 // CNSmlNotepadDataStore::CNSmlNotepadDataStore() 
       
   129 // ----------------------------------------------------------------------------
       
   130 CNSmlNotepadDataStore::CNSmlNotepadDataStore() : iOpenedStoreId(KNSmlNotepadAdapterImplUid),iKey( TKeyArrayFix( _FOFF( TNSmlSnapshotItem,ItemId() ),ECmpTInt ))
       
   131 	{
       
   132 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::CNSmlNotepadDataStore(): begin");
       
   133 	iSyncHistory = EFalse;
       
   134 	iDataBaseOpened = EFalse;
       
   135 	iReaderPosition = -1;
       
   136 	iWriterPosition = -1;
       
   137 	iModificationCount = KNSmlCompactAfterChanges;
       
   138 	iState = ENSmlClosed;
       
   139 	iSnapshotRegistered = EFalse;
       
   140 	iDrive = -1;
       
   141 	iReplaceItemId =  -1;
       
   142 	iServerMaxObjectSize = 0; // Maximum size default value
       
   143 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::CNSmlNotepadDataStore(): end");
       
   144 	}
       
   145 	
       
   146 // ----------------------------------------------------------------------------
       
   147 // CNSmlNotepadDataStore::ConstructL()
       
   148 // ----------------------------------------------------------------------------
       
   149 void CNSmlNotepadDataStore::ConstructL()
       
   150 	{
       
   151 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::ConstructL(): begin");
       
   152 	User::LeaveIfError(iRfs.Connect());
       
   153 	iStringPool.OpenL();
       
   154 	iUsedMimeType.Set( KMimePlainText() );
       
   155 	iUsedMimeVersion.Set( KMimePlainTextVersion() );
       
   156 	// Uid Sets
       
   157 	if(iNewUids)
       
   158 	{
       
   159 		delete iNewUids;
       
   160 		iNewUids = NULL;
       
   161 	}
       
   162 	iNewUids = new (ELeave) CNSmlDataItemUidSet();
       
   163 	if(iDeletedUids)
       
   164 	{
       
   165 		delete iDeletedUids;
       
   166 		iDeletedUids = NULL;
       
   167 	}
       
   168 	iDeletedUids = new (ELeave) CNSmlDataItemUidSet();
       
   169 	if(iSoftDeletedUids)
       
   170 	{
       
   171 		delete iSoftDeletedUids;
       
   172 		iSoftDeletedUids = NULL;
       
   173 	}
       
   174 	iSoftDeletedUids = new (ELeave) CNSmlDataItemUidSet();
       
   175 	if(iMovedUids)
       
   176 	{
       
   177 		delete iMovedUids;
       
   178 		iMovedUids = NULL;
       
   179 	}
       
   180 	iMovedUids = new (ELeave) CNSmlDataItemUidSet();
       
   181 	if(iReplacedUids)
       
   182 	{
       
   183 		delete iReplacedUids;
       
   184 		iReplacedUids = NULL;
       
   185 	}
       
   186 	iReplacedUids = new (ELeave) CNSmlDataItemUidSet();
       
   187 	if(iNpdDb)
       
   188 	{
       
   189 		delete iNpdDb;
       
   190 		iNpdDb = NULL;
       
   191 	}
       
   192 	iNpdDb = CNSmlNotepadDatabase::NewL();
       
   193 	if(iDefaultStoreName)
       
   194 	{
       
   195 		delete iDefaultStoreName;
       
   196 		iDefaultStoreName = NULL;
       
   197 	}
       
   198 	iDefaultStoreName = HBufC::NewL( KNSmlDefaultStoreNameMaxSize );
       
   199 	TInt len = iDefaultStoreName->Length();
       
   200 	TPtr obptr = iDefaultStoreName->Des();
       
   201 	iNpdDb->GetDefaultDatastoreName(obptr);
       
   202 	if(iOwnStoreFormat)
       
   203 	{
       
   204 		delete iOwnStoreFormat;
       
   205 		iOwnStoreFormat = NULL;
       
   206 	}
       
   207 	iOwnStoreFormat = DoOwnStoreFormatL();
       
   208 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::ConstructL(): end");
       
   209 	}
       
   210 
       
   211 
       
   212 // ----------------------------------------------------------------------------
       
   213 // void CNSmlNotepadDataStore::DefaultStoreNameL()
       
   214 // ----------------------------------------------------------------------------
       
   215 const TDesC& CNSmlNotepadDataStore::DefaultStoreNameL() const
       
   216 	{
       
   217 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DefaultStoreNameL(): begin");
       
   218 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DefaultStoreNameL(): end");
       
   219 	if ( iDefaultStoreName )
       
   220 		{
       
   221 			return *iDefaultStoreName;
       
   222 		}
       
   223 	return KNullDesC;
       
   224 	}
       
   225 
       
   226 
       
   227 // ----------------------------------------------------------------------------
       
   228 // CDesCArray* CNSmlNotepadDataStore::DoListStoresLC();
       
   229 // ----------------------------------------------------------------------------
       
   230 
       
   231 CDesCArray* CNSmlNotepadDataStore::DoListStoresLC()
       
   232 	{
       
   233 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoListStoresLC(): begin");
       
   234 	CDesCArray* npdStores = iNpdDb->ListDatabasesL();
       
   235 	CleanupStack::PushL( npdStores );
       
   236 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoListStoresLC(): end");
       
   237 	return npdStores;
       
   238 	}
       
   239 	
       
   240 // ----------------------------------------------------------------------------
       
   241 // void CNSmlNotepadDataStore::DoOpenL(const TDesC& 		  aStoreName, 
       
   242 //									    MSmlSyncRelationship& aContext,
       
   243 //									    TRequestStatus& 	  aStatus)
       
   244 // ----------------------------------------------------------------------------
       
   245 void CNSmlNotepadDataStore::DoOpenL( const TDesC& aStoreName, 
       
   246 									MSmlSyncRelationship& aContext, 
       
   247 									TRequestStatus& aStatus )
       
   248 {
       
   249 	iCallerStatus = &aStatus;
       
   250 	*iCallerStatus = KRequestPending;
       
   251 	if( iState != ENSmlClosed || iDataBaseOpened )
       
   252 	{
       
   253 		User::RequestComplete( iCallerStatus, KErrInUse );	
       
   254 		return;	
       
   255 	}
       
   256 	if(aStoreName.Length()>0)
       
   257 	{
       
   258 		if( RFs::CharToDrive(aStoreName[0], iDrive) != KErrNone )
       
   259 		{
       
   260 			RFs::CharToDrive( KNSmlDriveC()[0], iDrive );
       
   261 		}	
       
   262 	}
       
   263 		
       
   264 	TInt err;
       
   265 	TRAP(err,iNpdDb->OpenL( aStoreName ));
       
   266 	if( err == KErrNone )
       
   267 		{
       
   268 		if ( iOpenedStoreName )
       
   269 			{
       
   270 			delete iOpenedStoreName;
       
   271 			iOpenedStoreName = NULL;
       
   272 			}
       
   273 		iOpenedStoreName = aStoreName.Alloc();
       
   274 		
       
   275 		iDataBaseOpened = ETrue;
       
   276 		}
       
   277 	if ( iChangeFinder )
       
   278 		{
       
   279 		iChangeFinder->CloseL();
       
   280 		delete iChangeFinder;
       
   281 		iChangeFinder = NULL;
       
   282 		}
       
   283 	iChangeFinder = CNSmlChangeFinder::NewL( aContext, iKey, iSyncHistory, KNSmlNotepadAdapterImplUid );
       
   284 	
       
   285 	if ( iNotepadModsFetcher )
       
   286 		{
       
   287 		iNotepadModsFetcher->Cancel();
       
   288 		delete iNotepadModsFetcher;
       
   289 		iNotepadModsFetcher = NULL;
       
   290 		}
       
   291 	iState = ENSmlOpenAndWaiting;
       
   292 	
       
   293 	iNotepadModsFetcher = CNSmlNotepadModsFetcher::NewL( iSnapshotRegistered, iNpdDb, iKey, *iChangeFinder );
       
   294 	iNotepadModsFetcher->FetchModificationsL( aStatus );
       
   295 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenL(): end");
       
   296 }
       
   297 
       
   298 // ----------------------------------------------------------------------------
       
   299 // void CNSmlNotepadDataStore::DoCancelRequest()
       
   300 // ----------------------------------------------------------------------------
       
   301 void CNSmlNotepadDataStore::DoCancelRequest()
       
   302 	{
       
   303 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelRequest(): begin");
       
   304 	if(iNotepadModsFetcher && iNotepadModsFetcher->IsActive())
       
   305 	{
       
   306 		iNotepadModsFetcher->Cancel();
       
   307 	}
       
   308 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelRequest(): NOT NEEDED end");
       
   309 	}
       
   310 // ----------------------------------------------------------------------------
       
   311 // void CNSmlNotepadDataStore::DoStoreName()
       
   312 // ----------------------------------------------------------------------------
       
   313 const TDesC& CNSmlNotepadDataStore::DoStoreName() const
       
   314 	{
       
   315 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoStoreName(): begin");
       
   316 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoStoreName(): end");
       
   317 	if ( iOpenedStoreName )
       
   318 		{
       
   319 			return *iOpenedStoreName;
       
   320 		}
       
   321 	return KNullDesC;
       
   322 	}
       
   323 // ----------------------------------------------------------------------------
       
   324 // void CNSmlNotepadDataStore::DoBeginTransactionL()
       
   325 // ----------------------------------------------------------------------------
       
   326 void CNSmlNotepadDataStore::DoBeginTransactionL()
       
   327 	{
       
   328 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginTransactionL(): begin");
       
   329 	User::Leave( KErrNotSupported );
       
   330 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginTransactionL(): end");
       
   331 	}
       
   332 // ----------------------------------------------------------------------------
       
   333 // void CNSmlNotepadDataStore::DoCommitTransactionL()
       
   334 // ----------------------------------------------------------------------------
       
   335 void CNSmlNotepadDataStore::DoCommitTransactionL(TRequestStatus& aStatus)
       
   336 	{
       
   337 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitTransactionL(): begin");
       
   338 	iCallerStatus = &aStatus;
       
   339 	*iCallerStatus = KRequestPending;
       
   340 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   341 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitTransactionL(): end");
       
   342 	}
       
   343 // ----------------------------------------------------------------------------
       
   344 // void CNSmlNotepadDataStore::DoRevertTransaction()
       
   345 // ----------------------------------------------------------------------------
       
   346 void CNSmlNotepadDataStore::DoRevertTransaction(TRequestStatus& aStatus)
       
   347 	{
       
   348 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoRevertTransaction(): begin");
       
   349 	iCallerStatus = &aStatus;
       
   350 	*iCallerStatus = KRequestPending;
       
   351 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   352 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoRevertTransaction(): end");
       
   353 	}
       
   354 
       
   355 // ----------------------------------------------------------------------------
       
   356 // void CNSmlNotepadDataStore::DoBeginBatchL()
       
   357 // ----------------------------------------------------------------------------
       
   358 void CNSmlNotepadDataStore::DoBeginBatchL()
       
   359 	{
       
   360 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginBatchL(): begin");
       
   361 	User::Leave( KErrNotSupported );
       
   362 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginBatchL(): end");
       
   363 	}
       
   364 
       
   365 // ----------------------------------------------------------------------------
       
   366 // void CNSmlNotepadDataStore::DoCommitBatchL()
       
   367 // ----------------------------------------------------------------------------
       
   368 void CNSmlNotepadDataStore::DoCommitBatchL( RArray<TInt>& /*aResultArray*/, TRequestStatus& aStatus )
       
   369 	{
       
   370 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitBatchL(): begin");
       
   371 	iCallerStatus = &aStatus;
       
   372 	*iCallerStatus = KRequestPending;
       
   373 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   374 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitBatchL(): end");
       
   375 	}
       
   376 // ----------------------------------------------------------------------------
       
   377 // void CNSmlNotepadDataStore::DoCancelBatch()
       
   378 // ----------------------------------------------------------------------------
       
   379 void CNSmlNotepadDataStore::DoCancelBatch()
       
   380 	{
       
   381 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelBatch(): begin");
       
   382 
       
   383 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelBatch(): end");
       
   384 	}
       
   385 // ----------------------------------------------------------------------------
       
   386 // void CNSmlNotepadDataStore::DoSetRemoteStoreFormatL()
       
   387 // ----------------------------------------------------------------------------
       
   388 void CNSmlNotepadDataStore::DoSetRemoteStoreFormatL( const CSmlDataStoreFormat& aServerDataStoreFormat )
       
   389 	{
       
   390 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteStoreFormatL(): begin");
       
   391 	TBool foundMime = EFalse;
       
   392 	//
       
   393 	// KErrNotSupported is returned if server does not support same mime type 
       
   394 	// and same mime type version as Data Provider.
       
   395 	TInt i=0;
       
   396 	while( i< aServerDataStoreFormat.MimeFormatCount() && !foundMime )
       
   397 		{
       
   398 		if( iUsedMimeType.MatchF(aServerDataStoreFormat.MimeFormat(i).MimeType().DesC())      ==0 &&
       
   399 		    iUsedMimeVersion.MatchF(aServerDataStoreFormat.MimeFormat(i).MimeVersion().DesC())==0 )
       
   400 			{
       
   401 			foundMime = ETrue;
       
   402 			iServerMimeType    = aServerDataStoreFormat.MimeFormat(i).MimeType();
       
   403 			iServerMimeVersion = aServerDataStoreFormat.MimeFormat(i).MimeVersion();
       
   404 			}
       
   405 		i++;
       
   406 		}
       
   407 	if( !foundMime )
       
   408 		{
       
   409 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteStoreFormatL(): SERVER MIMETYPE NOTSUPPORTED end");
       
   410 		
       
   411 		User::Leave( KErrNotSupported );
       
   412 		}
       
   413 	if( !iOwnStoreFormat )
       
   414 		{
       
   415 			iOwnStoreFormat = DoOwnStoreFormatL();
       
   416 		}
       
   417 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteStoreFormatL(): end");
       
   418 	return;
       
   419 	}
       
   420 	
       
   421 // ----------------------------------------------------------------------------
       
   422 // void CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize()
       
   423 // ----------------------------------------------------------------------------
       
   424 void CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize( TInt aServerMaxObjectSize )
       
   425 	{
       
   426 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize(): begin");
       
   427 	iServerMaxObjectSize = aServerMaxObjectSize;
       
   428 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize(): end");
       
   429 	}	
       
   430 // ----------------------------------------------------------------------------
       
   431 // void CNSmlNotepadDataStore::DoMaxObjectSize()
       
   432 // ----------------------------------------------------------------------------
       
   433 TInt CNSmlNotepadDataStore::DoMaxObjectSize() const
       
   434 	{
       
   435 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMaxObjectSize(): begin");
       
   436 	if ( iOwnStoreFormat )
       
   437 		{
       
   438 		if ( iOwnStoreFormat->HasMaxSize() )
       
   439 			{
       
   440 			_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMaxObjectSize - From resource: END");
       
   441 			
       
   442 			return iOwnStoreFormat->MaxSize();
       
   443 			}
       
   444 		}
       
   445 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMaxObjectSize() 100 k: end");
       
   446 	return KNSmlDataStoreMaxSize;  // 100 k
       
   447 	}
       
   448 		
       
   449 
       
   450 // ----------------------------------------------------------------------------
       
   451 // void CNSmlNotepadDataStore::DoOpenItemL()
       
   452 // ----------------------------------------------------------------------------
       
   453 void CNSmlNotepadDataStore::DoOpenItemL( TSmlDbItemUid aUid, 
       
   454 										  TBool& aFieldChange, 
       
   455 										  TInt& aSize, 
       
   456 										  TSmlDbItemUid& aParent, 
       
   457 										  TDes8& aMimeType, 
       
   458 										  TDes8& aMimeVer, 
       
   459 										  TRequestStatus& aStatus )
       
   460 	{
       
   461 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenItemL(): begin");
       
   462 	iCallerStatus = &aStatus;
       
   463 	*iCallerStatus = KRequestPending;
       
   464 	if ( iState != ENSmlOpenAndWaiting )
       
   465 		{
       
   466 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   467 		return;
       
   468 		}
       
   469 	aFieldChange = EFalse; // Whole item sent NOT SUPPORT ON FIELDCHANGE
       
   470 	aMimeType    = iOwnStoreFormat->MimeFormat( 0 ).MimeType().DesC();
       
   471 	aMimeVer	 = iOwnStoreFormat->MimeFormat( 0 ).MimeVersion().DesC();
       
   472 	aParent      = KNullDataItemId;
       
   473 	if( !iItemData )
       
   474 		{
       
   475 		iItemData= CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   476 		}
       
   477 	else
       
   478 		{
       
   479 		iItemData->Reset();
       
   480 		}
       
   481 	TInt err = OpenItemL( aUid, *iItemData );
       
   482 	if( !err )
       
   483 		{
       
   484 		
       
   485 		iItemData->Compress();
       
   486 		iReaderPosition  = 0;
       
   487 		aSize = iItemData->Size();
       
   488 		iState = ENSmlItemOpen;
       
   489 		if ( iServerMaxObjectSize == 0 || aSize <= iServerMaxObjectSize )
       
   490 			{
       
   491 			User::RequestComplete( iCallerStatus, KErrNone );
       
   492 			}
       
   493 		else
       
   494 			{
       
   495 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   496 			}
       
   497 		}
       
   498 	else
       
   499 		{
       
   500 		User::RequestComplete( iCallerStatus, err );
       
   501 		}
       
   502 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenItemL(): end");
       
   503 	}
       
   504 
       
   505 // ----------------------------------------------------------------------------
       
   506 // void CNSmlNotepadDataStore::DoCreateItemL()
       
   507 // ----------------------------------------------------------------------------
       
   508 void CNSmlNotepadDataStore::DoCreateItemL( TSmlDbItemUid& aUid, 
       
   509 											TInt aSize, 
       
   510 											TSmlDbItemUid /*aParent*/, 
       
   511 											const TDesC8& aMimeType, 
       
   512 											const TDesC8& aMimeVer, 
       
   513 											TRequestStatus& aStatus )
       
   514 	{
       
   515 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCreateItemL(): begin");
       
   516 	TBuf8<255> lMimeType;
       
   517 	lMimeType.Copy(aMimeType);
       
   518 	TBuf8<255> lMimeVer;
       
   519 	lMimeVer.Copy(aMimeVer);
       
   520 	iCallerStatus = &aStatus;
       
   521 	*iCallerStatus = KRequestPending;
       
   522 	iAddItemId = &aUid;
       
   523 	if ( iState != ENSmlOpenAndWaiting )
       
   524 		{
       
   525 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   526 		return;
       
   527 		}
       
   528 	if ( iOwnStoreFormat->HasMaxSize() )
       
   529 		{
       
   530 		if ( iOwnStoreFormat->MaxSize() < aSize )
       
   531 			{
       
   532 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   533 			return;
       
   534 			}
       
   535 		}
       
   536 	if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive ) )
       
   537 		{
       
   538 		User::RequestComplete( iCallerStatus, KErrDiskFull );
       
   539 		return;
       
   540 		}
       
   541 	TBuf8<255> lOwnMimeType;
       
   542 	lOwnMimeType.Copy(iOwnStoreFormat->MimeFormat( 0 ).MimeType().DesC());
       
   543 	TBuf8<255> lOwnMimeVer;
       
   544 	lOwnMimeVer.Copy(iOwnStoreFormat->MimeFormat( 0 ).MimeVersion().DesC());	
       
   545 	if ( 	(iOwnStoreFormat->MimeFormat( 0 ).MimeType().DesC().Compare( aMimeType ) != 0 ) )
       
   546 		{
       
   547 		User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   548 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCreateItemL - KErrNotSupported: END");
       
   549 		return;
       
   550 		}	
       
   551 	if ( iItemData )
       
   552 		{
       
   553 		delete iItemData;
       
   554 		iItemData = NULL;
       
   555 		}
       
   556 	iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   557 	iWriterPosition  = 0;
       
   558 	iItemSize = aSize; // Size sent from server
       
   559 	iState = ENSmlItemCreating;
       
   560 	User::RequestComplete( iCallerStatus, KErrNone );
       
   561 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCreateItemL(): end");
       
   562 	}
       
   563 
       
   564 // ----------------------------------------------------------------------------
       
   565 // void CNSmlNotepadDataStore::DoReplaceItemL()
       
   566 // ----------------------------------------------------------------------------
       
   567 void CNSmlNotepadDataStore::DoReplaceItemL( TSmlDbItemUid aUid, 
       
   568 											 TInt aSize, 
       
   569 											 TSmlDbItemUid /*aParent*/, 
       
   570 											 TBool aFieldChange, 
       
   571 											 TRequestStatus& aStatus )
       
   572 	{
       
   573 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemL(): begin");
       
   574 	iCallerStatus = &aStatus;
       
   575 	*iCallerStatus = KRequestPending;
       
   576 	if ( iState != ENSmlOpenAndWaiting )
       
   577 		{
       
   578 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   579 		return;
       
   580 		}
       
   581 	if ( iOwnStoreFormat->HasMaxSize() )
       
   582 		{
       
   583 		if ( iOwnStoreFormat->MaxSize() < aSize )
       
   584 			{
       
   585 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   586 			return;
       
   587 			}
       
   588 		}
       
   589 	if( aFieldChange )
       
   590 		{
       
   591 		User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   592 		return;
       
   593 		}
       
   594 	if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive ) )
       
   595 		{
       
   596 		User::RequestComplete(iCallerStatus, KErrDiskFull);
       
   597 		return;
       
   598 		}
       
   599 	TInt err = KErrNone;
       
   600 	iUid      = aUid;
       
   601 	iItemSize = aSize;
       
   602 	iWriterPosition  = 0;
       
   603 	if ( iItemData )
       
   604 		{
       
   605 		delete iItemData;
       
   606 		iItemData = NULL;
       
   607 		}
       
   608 	iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );	
       
   609 	CNpdItem *note;
       
   610     note = CNpdItem::NewLC();
       
   611     TInt itemFound;
       
   612     TRAP(itemFound,iNpdDb->GetNoteL(aUid, *note));	
       
   613 	CleanupStack::PopAndDestroy(note);//for note
       
   614 	if ( itemFound != KErrNone)
       
   615 		{
       
   616 		delete iItemData;
       
   617 		iItemData = NULL;
       
   618 		
       
   619 		User::RequestComplete( iCallerStatus, KErrNotFound );
       
   620 		return;
       
   621 		}
       
   622 	iReplaceItemId = aUid;
       
   623 	iState = ENSmlItemUpdating;
       
   624 	User::RequestComplete( iCallerStatus, err );
       
   625 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemL(): end");
       
   626 	}
       
   627 // ----------------------------------------------------------------------------
       
   628 // void CNSmlNotepadDataStore::DoReadItemL()
       
   629 // ----------------------------------------------------------------------------
       
   630 void CNSmlNotepadDataStore::DoReadItemL( TDes8& aBuffer )
       
   631 	{
       
   632 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReadItemL(): begin");
       
   633     if ( iState != ENSmlItemOpen || !iItemData )
       
   634         {
       
   635         iReaderPosition  = -1;
       
   636         User::Leave( KErrNotReady );
       
   637         }
       
   638     if ( iReaderPosition  == -1 )
       
   639         {
       
   640           User::Leave( KErrEof );
       
   641         }
       
   642 
       
   643 	if ( aBuffer.MaxSize() <= iItemData->Size() )
       
   644 		{
       
   645 		//iItemData->Read( 0, aBuffer );
       
   646 		//iReaderPosition  = -1;
       
   647 		if (iReaderPosition < (iItemData->Size() - iReaderPosition))
       
   648 		{
       
   649 			iItemData->Read( iReaderPosition , aBuffer);
       
   650 			iReaderPosition  = iReaderPosition  + aBuffer.Size();
       
   651 		}
       
   652 		else
       
   653 		{
       
   654 			if (aBuffer.MaxLength() <= (iItemData->Size() - iReaderPosition))
       
   655 			{
       
   656 				iItemData->Read( iReaderPosition , aBuffer, aBuffer.MaxLength() );
       
   657 			}
       
   658 			else
       
   659 			{
       
   660 				iItemData->Read( iReaderPosition , aBuffer, (iItemData->Size() - iReaderPosition) );
       
   661 			}
       
   662 			iReaderPosition  = -1;
       
   663 		}
       
   664 
       
   665 		}
       
   666 	else
       
   667 		{
       
   668 		TInt length = iItemData->Size() - iReaderPosition;
       
   669 		if ( aBuffer.Size() <= length )
       
   670 			{
       
   671 			iItemData->Read( iReaderPosition , aBuffer );
       
   672 			iReaderPosition  = -1;
       
   673 			}
       
   674 		else
       
   675 			{
       
   676 			iItemData->Read( iReaderPosition , aBuffer, aBuffer.Size() );
       
   677 			iReaderPosition  = iReaderPosition  + aBuffer.Size();
       
   678 			}
       
   679 		}
       
   680 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReadItemL(): end");
       
   681 	}
       
   682 
       
   683 // ----------------------------------------------------------------------------
       
   684 // void CNSmlNotepadDataStore::DoWriteItemL()
       
   685 // ----------------------------------------------------------------------------
       
   686 void CNSmlNotepadDataStore::DoWriteItemL( const TDesC8& aData )
       
   687 	{
       
   688 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoWriteItemL(): begin");
       
   689 	if ( iState == ENSmlItemCreating || iState == ENSmlItemUpdating )
       
   690 		{
       
   691 		TInt totalSize = aData.Size() + iItemData->Size();
       
   692 		if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, totalSize, iDrive ) )
       
   693 			{
       
   694 			User::RequestComplete(iCallerStatus, KErrDiskFull);
       
   695 			return;
       
   696 			}
       
   697 		if ( iItemData )
       
   698 			{
       
   699 			if ( iWriterPosition  == -1 )
       
   700 				{
       
   701 				
       
   702 				User::Leave( KErrEof );
       
   703 				}
       
   704 			iItemData->InsertL( iWriterPosition , aData );
       
   705 			iWriterPosition  = aData.Size();
       
   706 			return;
       
   707 			}
       
   708 		}
       
   709 	User::Leave( KErrNotReady );
       
   710 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoWriteItemL(): end");
       
   711 	}
       
   712 
       
   713 // ----------------------------------------------------------------------------
       
   714 // void CNSmlNotepadDataStore::DoCommitItemL()
       
   715 // ----------------------------------------------------------------------------
       
   716 void CNSmlNotepadDataStore::DoCommitItemL( TRequestStatus& aStatus )
       
   717 	{
       
   718 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitItemL(): begin");	
       
   719 	iCallerStatus = &aStatus;
       
   720 	*iCallerStatus = KRequestPending;
       
   721 	if ( iState == ENSmlItemCreating || iState == ENSmlItemUpdating )
       
   722 		{
       
   723 		if ( !iItemData )
       
   724 			{
       
   725 			User::RequestComplete( iCallerStatus, KErrNotReady );
       
   726 			return;
       
   727 			}
       
   728 		}
       
   729 	else
       
   730 		{
       
   731 		User::RequestComplete(iCallerStatus, KErrNotReady );
       
   732 		return;
       
   733 		}
       
   734 	iItemData->Compress();
       
   735 	TInt err(KErrNone);
       
   736 	if ( iState == ENSmlItemCreating )
       
   737 		{
       
   738 		iState = ENSmlClosed; 
       
   739 		TSmlDbItemUid newUid; 
       
   740 		err = DoAddItemToDataBaseL( *iItemData,newUid);
       
   741 		*iAddItemId = newUid;
       
   742 		}
       
   743 	else // ENSmlItemUpdating
       
   744 		{
       
   745 		iState = ENSmlClosed; 
       
   746 		err = DoReplaceItemAtDataBaseL( *iItemData, iReplaceItemId );
       
   747 		}
       
   748 	iReplaceItemId = -1;
       
   749 	iWriterPosition  = -1;
       
   750 	iState = ENSmlOpenAndWaiting;
       
   751 	if ( iItemData )
       
   752 		{
       
   753 		delete iItemData;
       
   754 		iItemData = NULL;
       
   755 		}
       
   756 	User::RequestComplete( iCallerStatus, err);
       
   757 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitItemL(): end");
       
   758 	}
       
   759 
       
   760 // ----------------------------------------------------------------------------
       
   761 // void CNSmlNotepadDataStore::DoCloseItem()
       
   762 // ----------------------------------------------------------------------------
       
   763 void CNSmlNotepadDataStore::DoCloseItem()
       
   764 	{
       
   765 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCloseItem(): begin");
       
   766 	if ( iState == ENSmlItemOpen )
       
   767 		{
       
   768 		iReaderPosition  = -1;
       
   769 		if ( iItemData )
       
   770 			{
       
   771 			delete iItemData;
       
   772 			iItemData = NULL;
       
   773 			}
       
   774 		iState = ENSmlOpenAndWaiting;
       
   775 		}
       
   776 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCloseItem(): end");
       
   777 	}
       
   778 
       
   779 
       
   780 // ----------------------------------------------------------------------------
       
   781 // void CNSmlNotepadDataStore::DoMoveItemL()
       
   782 // ----------------------------------------------------------------------------
       
   783 void CNSmlNotepadDataStore::DoMoveItemL( TSmlDbItemUid /*aUid*/, 
       
   784 										TSmlDbItemUid /*aNewParent*/, 
       
   785 										TRequestStatus& aStatus )
       
   786 	{
       
   787 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMoveItemL(): begin");
       
   788 	iCallerStatus = &aStatus;
       
   789 	*iCallerStatus = KRequestPending;
       
   790 	if ( iState != ENSmlOpenAndWaiting )
       
   791 		{
       
   792 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   793 		return;
       
   794 		}
       
   795 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   796 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMoveItemL(): end");
       
   797 	}
       
   798 
       
   799 // ----------------------------------------------------------------------------
       
   800 // void CNSmlNotepadDataStore::DoDeleteItemL()
       
   801 // ----------------------------------------------------------------------------
       
   802 void CNSmlNotepadDataStore::DoDeleteItemL( TSmlDbItemUid aUid, TRequestStatus& aStatus )
       
   803 	{
       
   804 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteItemL(): begin");
       
   805 	iCallerStatus = &aStatus;
       
   806 	*iCallerStatus = KRequestPending;
       
   807 	if ( iState != ENSmlOpenAndWaiting ) 
       
   808 		{
       
   809 		// Items cannot be deleted if some of them is opened
       
   810 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   811 		return;
       
   812 		}
       
   813 	iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   814 	CNpdItem *note;
       
   815     note = CNpdItem::NewLC();
       
   816     TInt itemFound;
       
   817     TRAP(itemFound,iNpdDb->GetNoteL(aUid, *note));	
       
   818     CleanupStack::PopAndDestroy(note); // for note
       
   819 	if( itemFound != KErrNone)
       
   820 		{
       
   821 		delete iItemData;
       
   822 		iItemData = NULL;
       
   823 		
       
   824 		User::RequestComplete( iCallerStatus, KErrNotFound );
       
   825 		return;
       
   826 		}
       
   827 	TRAPD( err, iNpdDb->DeleteNoteL( aUid ) );
       
   828 	if( err == KErrNone )
       
   829 		{
       
   830 			if(iChangeFinder)
       
   831 			{
       
   832 				iChangeFinder->ItemDeleted( TNSmlSnapshotItem( aUid ) );
       
   833 			}
       
   834 	
       
   835 		}
       
   836 	User::RequestComplete(iCallerStatus, err);
       
   837 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteItemL(): end");
       
   838 	}
       
   839 
       
   840 // ----------------------------------------------------------------------------
       
   841 // void CNSmlNotepadDataStore::DoSoftDeleteItemL()
       
   842 // ----------------------------------------------------------------------------
       
   843 void CNSmlNotepadDataStore::DoSoftDeleteItemL( TSmlDbItemUid aUid, TRequestStatus& aStatus )
       
   844 	{
       
   845 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeleteItemL(): begin");
       
   846 	DoDeleteItemL( aUid,aStatus );
       
   847 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeleteItemL(): end");
       
   848 	}
       
   849 
       
   850 // ----------------------------------------------------------------------------
       
   851 // void CNSmlNotepadDataStore::DoDeleteAllItemsL()
       
   852 // ----------------------------------------------------------------------------
       
   853 void CNSmlNotepadDataStore::DoDeleteAllItemsL( TRequestStatus& aStatus )
       
   854 	{
       
   855 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteAllItemsL(): begin");
       
   856 	iCallerStatus = &aStatus;
       
   857 	*iCallerStatus = KRequestPending;
       
   858 	if ( iState != ENSmlOpenAndWaiting ) 
       
   859 		{
       
   860 		// Items cannot be deleted if they are opened.
       
   861 		User::RequestComplete(iCallerStatus, KErrNotReady);
       
   862 		return;
       
   863 		}
       
   864 	iNpdDb->Reset();
       
   865 	if( iChangeFinder )
       
   866 		{
       
   867 		iChangeFinder->ResetL();		
       
   868 		}
       
   869 	iSnapshotRegistered = EFalse;
       
   870 	User::RequestComplete(iCallerStatus, KErrNone);
       
   871 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteAllItemsL(): end");
       
   872 	}
       
   873 
       
   874 // ----------------------------------------------------------------------------
       
   875 // TBool CNSmlNotepadDataStore::DoHasSyncHistory()
       
   876 // ----------------------------------------------------------------------------
       
   877 TBool CNSmlNotepadDataStore::DoHasSyncHistory() const
       
   878 	{
       
   879 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoHasSyncHistory(): begin");
       
   880 	TBool doHasSyncHistory = EFalse;
       
   881 	if( !iDataBaseOpened ) 
       
   882 		{
       
   883 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoHasSyncHistory(): NOT OPEN end");
       
   884 		return doHasSyncHistory;
       
   885 		}
       
   886 
       
   887 	if( iSyncHistory )
       
   888 		{
       
   889 		if ( iOpenedStoreId != iChangeFinder->DataStoreUid() )
       
   890 			{
       
   891 			iChangeFinder->SetDataStoreUid( iOpenedStoreId );
       
   892 			doHasSyncHistory = EFalse;
       
   893 			}
       
   894 		else
       
   895 			{
       
   896 				doHasSyncHistory = ETrue;
       
   897 			}
       
   898 		}
       
   899 	else
       
   900 		{
       
   901 		iChangeFinder->SetDataStoreUid( iOpenedStoreId );
       
   902 		}
       
   903 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoHasSyncHistory(): end");
       
   904 	return doHasSyncHistory;
       
   905 	}
       
   906 
       
   907 // ----------------------------------------------------------------------------
       
   908 // void CNSmlNotepadDataStore::DoAddedItems()
       
   909 // ----------------------------------------------------------------------------
       
   910 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoAddedItems() const
       
   911 	{
       
   912 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddedItems(): begin");
       
   913 	if ( iState == ENSmlOpenAndWaiting )
       
   914 		{
       
   915 		iNewUids->Reset();
       
   916 		if( iChangeFinder )
       
   917 			{
       
   918 			TRAPD( err,iChangeFinder->FindNewItemsL( *iNewUids ) );
       
   919 			if( err != KErrNone )
       
   920 				{
       
   921 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddedItems(): LEAVE end");				
       
   922 				}
       
   923 			}
       
   924 		}	
       
   925 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddedItems(): end");
       
   926 	return *iNewUids;
       
   927 	}
       
   928 
       
   929 // ----------------------------------------------------------------------------
       
   930 // void CNSmlNotepadDataStore::DoDeletedItems()
       
   931 // ----------------------------------------------------------------------------
       
   932 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoDeletedItems() const
       
   933 	{
       
   934 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeletedItems(): begin");	
       
   935 	if ( iState == ENSmlOpenAndWaiting )
       
   936 		{
       
   937 		iDeletedUids->Reset();
       
   938 		if( iChangeFinder )
       
   939 			{
       
   940 			TRAPD(err,iChangeFinder->FindDeletedItemsL( *iDeletedUids ) );
       
   941 			if( err != KErrNone )
       
   942 				{
       
   943 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeletedItems(): LEAVE end");				
       
   944 				}
       
   945 			}
       
   946 		}
       
   947 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeletedItems(): end");
       
   948 	return *iDeletedUids;
       
   949 	}
       
   950 
       
   951 // ----------------------------------------------------------------------------
       
   952 // void CNSmlNotepadDataStore::DoSoftDeletedItems()
       
   953 // ----------------------------------------------------------------------------
       
   954 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoSoftDeletedItems() const
       
   955 	{
       
   956 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeletedItems(): begin");
       
   957 	if ( iState == ENSmlOpenAndWaiting )
       
   958 		{
       
   959 		iSoftDeletedUids->Reset();
       
   960 		if( iChangeFinder )
       
   961 			{
       
   962 			TRAPD( err, iChangeFinder->FindSoftDeletedItemsL( *iSoftDeletedUids ) );
       
   963 			if( err != KErrNone )
       
   964 				{
       
   965 				
       
   966 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeletedItems(): LEAVE end");				
       
   967 				}
       
   968 			}
       
   969 		}
       
   970 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeletedItems: END");
       
   971 	return *iSoftDeletedUids;
       
   972 	}
       
   973 
       
   974 // ----------------------------------------------------------------------------
       
   975 // void CNSmlNotepadDataStore::DoModifiedItems()
       
   976 // ----------------------------------------------------------------------------
       
   977 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoModifiedItems() const
       
   978 	{
       
   979 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoModifiedItems(): begin");
       
   980 	if ( iState == ENSmlOpenAndWaiting )
       
   981 		{
       
   982 		iReplacedUids->Reset();
       
   983 		if( iChangeFinder )
       
   984 			{
       
   985 			TRAPD( err, iChangeFinder->FindChangedItemsL( *iReplacedUids ) );
       
   986 			if( err != KErrNone )
       
   987 				{
       
   988 					_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoModifiedItems(): LEAVE end");				
       
   989 				}
       
   990 			}
       
   991 		}
       
   992 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoModifiedItems: END");
       
   993 	return *iReplacedUids;
       
   994 	}
       
   995 
       
   996 // ----------------------------------------------------------------------------
       
   997 // void CNSmlNotepadDataStore::DoMovedItems()
       
   998 // ----------------------------------------------------------------------------
       
   999 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoMovedItems() const
       
  1000 	{
       
  1001 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMovedItems(): begin");
       
  1002 	if ( iState == ENSmlOpenAndWaiting )
       
  1003 		{
       
  1004 		
       
  1005 		iMovedUids->Reset();
       
  1006 		
       
  1007 		if( iChangeFinder )
       
  1008 			{
       
  1009 			
       
  1010 			TRAPD( err, iChangeFinder->FindMovedItemsL( *iMovedUids ) );	
       
  1011 			if( err != KErrNone )
       
  1012 				{
       
  1013 				
       
  1014 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMovedItems(): LEAVE end");				
       
  1015 				}
       
  1016 			}
       
  1017 		}
       
  1018 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMovedItems: END");
       
  1019 	return *iMovedUids;
       
  1020 	}
       
  1021 
       
  1022 // ----------------------------------------------------------------------------
       
  1023 // void CNSmlNotepadDataStore::DoResetChangeInfoL()
       
  1024 // ----------------------------------------------------------------------------
       
  1025 void CNSmlNotepadDataStore::DoResetChangeInfoL( TRequestStatus& aStatus )
       
  1026 	{
       
  1027 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoResetChangeInfoL(): begin");
       
  1028 	iCallerStatus = &aStatus;
       
  1029 	*iCallerStatus = KRequestPending;
       
  1030 	if( iChangeFinder )
       
  1031 		{
       
  1032 		iChangeFinder->ResetL();
       
  1033 		}
       
  1034 	iSnapshotRegistered = EFalse;
       
  1035 	if ( iNotepadModsFetcher )
       
  1036 		{
       
  1037 		iNotepadModsFetcher->Reset();
       
  1038 		iNotepadModsFetcher->FetchModificationsL( aStatus );
       
  1039 		}
       
  1040 	else
       
  1041 		{
       
  1042 		User::RequestComplete( iCallerStatus, KErrNone );
       
  1043 		}
       
  1044 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoResetChangeInfoL(): end");
       
  1045 	}
       
  1046 
       
  1047 // ----------------------------------------------------------------------------
       
  1048 // void CNSmlNotepadDataStore::DoCommitChangeInfoL()
       
  1049 // ----------------------------------------------------------------------------
       
  1050 void CNSmlNotepadDataStore::DoCommitChangeInfoL( TRequestStatus& aStatus, 
       
  1051 												  const MSmlDataItemUidSet& aItems )
       
  1052 	{
       
  1053 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(aItems): begin");
       
  1054 	iCallerStatus = &aStatus;
       
  1055 	*iCallerStatus = KRequestPending;
       
  1056 	if( iChangeFinder )
       
  1057 		{
       
  1058 		iChangeFinder->CommitChangesL( aItems );
       
  1059 		User::RequestComplete( iCallerStatus, KErrNone );
       
  1060 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(aItems):success end");
       
  1061 		return;
       
  1062 		}
       
  1063 	User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1064 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(aItems): end");
       
  1065 	}
       
  1066 
       
  1067 // ----------------------------------------------------------------------------
       
  1068 // void CNSmlNotepadDataStore::DoCommitChangeInfoL()
       
  1069 // ----------------------------------------------------------------------------
       
  1070 void CNSmlNotepadDataStore::DoCommitChangeInfoL( TRequestStatus& aStatus )
       
  1071 	{
       
  1072 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(): begin");
       
  1073 	iCallerStatus = &aStatus;
       
  1074 	*iCallerStatus = KRequestPending;
       
  1075 	if( iChangeFinder )
       
  1076 		{
       
  1077 		iChangeFinder->CommitChangesL();
       
  1078 		User::RequestComplete( iCallerStatus, KErrNone );
       
  1079 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(): success end");
       
  1080 		return;
       
  1081 		}
       
  1082 	User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1083 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(): end");
       
  1084 	}
       
  1085 
       
  1086 
       
  1087 
       
  1088 
       
  1089 // ----------------------------------------------------------------------------
       
  1090 // CSmlDataStoreFormat* CNSmlNotepadDataStore::DoOwnStoreFormatL()
       
  1091 // ----------------------------------------------------------------------------
       
  1092 CSmlDataStoreFormat* CNSmlNotepadDataStore::DoOwnStoreFormatL()
       
  1093 {
       
  1094 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOwnStoreFormatL(): begin");
       
  1095 	TFileName fileName;
       
  1096 	TParse parse;
       
  1097 	parse.Set( KNSmlNotepadStoreFormatRsc, &KDC_RESOURCE_FILES_DIR, NULL );
       
  1098 	fileName = parse.FullName();
       
  1099 	RResourceFile resourceFile; 
       
  1100 	BaflUtils::NearestLanguageFile( iRfs, fileName );
       
  1101 	resourceFile.OpenL( iRfs, fileName );
       
  1102 	CleanupClosePushL( resourceFile );
       
  1103 	HBufC8* notepadDataFormat = resourceFile.AllocReadLC( NSML_NOTEPAD_DATA_STORE_FORMAT );
       
  1104 	TResourceReader reader;
       
  1105 	reader.SetBuffer( notepadDataFormat );
       
  1106 	CSmlDataStoreFormat* lDsFormat = CSmlDataStoreFormat::NewLC( iStringPool, reader );
       
  1107 	CleanupStack::Pop(lDsFormat); // lDsFormat
       
  1108 	CleanupStack::PopAndDestroy(notepadDataFormat); // notepadDataFormat
       
  1109 	CleanupStack::PopAndDestroy();// resourceFile 
       
  1110 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOwnStoreFormatL(): end");	
       
  1111 	return lDsFormat; 
       
  1112 	
       
  1113 }
       
  1114 
       
  1115 // ----------------------------------------------------------------------------
       
  1116 // TInt CNSmlNotepadDataStore::OpenItemL(TSmlDbItemUid& aUid,CBufBase& aItem)
       
  1117 // ----------------------------------------------------------------------------
       
  1118 TInt CNSmlNotepadDataStore::OpenItemL(TSmlDbItemUid& aUid,CBufBase& aItem)
       
  1119 {
       
  1120 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::OpenItemL(): begin");
       
  1121 	TInt err = KErrNone;
       
  1122 	if(iNpdDb->IsOpen())
       
  1123         {
       
  1124         CNpdItem *note;
       
  1125         note = CNpdItem::NewLC();
       
  1126         TRAP(err,iNpdDb->GetNoteL(aUid, *note));
       
  1127         if(err == KErrNone)
       
  1128             {
       
  1129             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore:: FetchItemL():  begin");
       
  1130             
       
  1131             HBufC8* buf = ConvertNoteToSyncItemL(*note);
       
  1132             CleanupStack::PushL(buf);
       
  1133             aItem.Reset();
       
  1134             // Write the buffer to the passed writeStream (aItem)
       
  1135             TBufBuf bufBuf;
       
  1136             bufBuf.Set(aItem, 0, TBufBuf::EWrite);
       
  1137             RWriteStream writeStream( &bufBuf);
       
  1138             writeStream.PushL();
       
  1139             writeStream.WriteL(buf->Des().Ptr(), buf->Des().Length());
       
  1140             writeStream.CommitL();
       
  1141             CleanupStack::PopAndDestroy(1);//writeStream
       
  1142             CleanupStack::PopAndDestroy(buf);//buf
       
  1143             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchItemL(): end");
       
  1144             }
       
  1145         CleanupStack::PopAndDestroy(note); // note
       
  1146         }
       
  1147     else
       
  1148         {
       
  1149         err = KErrNotReady;
       
  1150         }
       
  1151         
       
  1152 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::OpenItemL(): end");
       
  1153 	return err;
       
  1154 }
       
  1155 
       
  1156 // ----------------------------------------------------------------------------
       
  1157 // TInt CNSmlNotepadDataStore::ConvertSyncItemToNoteL(const TDesC8& aSyncItem, CNpdItem& aNote)
       
  1158 // ----------------------------------------------------------------------------
       
  1159 TInt CNSmlNotepadDataStore::ConvertSyncItemToNoteL(const TDesC8& aSyncItem, CNpdItem& aNote)
       
  1160     {
       
  1161     TInt err(KErrNone);
       
  1162     TTime gmt;
       
  1163     gmt.UniversalTime();
       
  1164     gmt.RoundUpToNextMinute();
       
  1165     gmt -= TTimeIntervalMinutes(1);
       
  1166     HBufC *note = NULL;
       
  1167     if(&aSyncItem)
       
  1168         {
       
  1169         RDesReadStream stream(aSyncItem);
       
  1170         CPlainText *text;
       
  1171         text = CPlainText::NewL();
       
  1172         CleanupStack::PushL(text);
       
  1173         CPlainText::TImportExportParam param;
       
  1174         param.iForeignEncoding = KCharacterSetIdentifierUtf8;
       
  1175         param.iOrganisation = CPlainText::EOrganiseByParagraph;
       
  1176         param.iGuessForeignEncoding = EFalse;
       
  1177         CPlainText::TImportExportResult result;
       
  1178         text->ImportTextL(0,stream,param,result);
       
  1179         stream.Close();
       
  1180         note = HBufC::NewL(result.iOutputChars);
       
  1181         CleanupStack::PushL(note);
       
  1182         TPtr tmpdes = note->Des();
       
  1183         text->Extract(tmpdes,0);
       
  1184         CleanupStack::Pop(note); 
       
  1185         CleanupStack::PopAndDestroy(text); 
       
  1186         }
       
  1187     aNote.SetContent(note); 
       
  1188     aNote.SetModified(gmt);
       
  1189     return err;
       
  1190     }
       
  1191 // ----------------------------------------------------------------------------
       
  1192 // HBufC8* CNSmlNotepadDataStore::ConvertNoteToSyncItemL(CNpdItem& aNote)
       
  1193 // ----------------------------------------------------------------------------
       
  1194 HBufC8* CNSmlNotepadDataStore::ConvertNoteToSyncItemL(CNpdItem& aNote)
       
  1195     {
       
  1196     if (!aNote.Content())
       
  1197         {
       
  1198         HBufC8* tempHBuf = HBufC8::NewL(KNullCharLen);//room for null character
       
  1199         return tempHBuf;
       
  1200         }
       
  1201     // Note: a single unicode character can be 4 bytes long in UTF8 format,
       
  1202     // hence the long length for 8-bit buffer.
       
  1203     TInt length = (aNote.Content()->Length()) * KUTF8Lenght;
       
  1204     HBufC8* tempHBuf = HBufC8::NewLC(length + KNullCharLen);
       
  1205     CPlainText *text = CPlainText::NewL();
       
  1206     CleanupStack::PushL(text);
       
  1207     text->InsertL(0,aNote.Content()->Des());
       
  1208     CPlainText::TImportExportResult result;
       
  1209     CPlainText::TImportExportParam param;
       
  1210     param.iForeignEncoding = KCharacterSetIdentifierUtf8;
       
  1211     param.iOrganisation = CPlainText::EOrganiseByParagraph;
       
  1212     param.iGuessForeignEncoding = EFalse;
       
  1213     param.iMaxOutputChars = length;
       
  1214     TPtr8 ptr = tempHBuf->Des();
       
  1215     RDesWriteStream stream(ptr);
       
  1216     text->ExportTextL(0,stream,param,result);
       
  1217     stream.Close();
       
  1218     CleanupStack::PopAndDestroy(text);
       
  1219     CleanupStack::Pop(tempHBuf);//pop the HBufC8 temp 
       
  1220     return (tempHBuf);
       
  1221     }
       
  1222 // ----------------------------------------------------------------------------
       
  1223 // TInt CNSmlNotepadDataStore::DoAddItemToDataBaseL(CBufBase& aItem)
       
  1224 // ----------------------------------------------------------------------------
       
  1225 TInt CNSmlNotepadDataStore::DoAddItemToDataBaseL(CBufBase& aItem,TInt& aUid)
       
  1226 {
       
  1227 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: begin");
       
  1228 	TInt lKey = -1;
       
  1229 	TInt length = aItem.Size();
       
  1230     HBufC8* buf = HBufC8::NewLC(length*KUnicodeSize + KNullCharLen);
       
  1231     buf->Des().Copy(aItem.Ptr(0));
       
  1232     TPtr8 lPtr(buf->Des());
       
  1233     CNpdItem *note;
       
  1234     note = CNpdItem::NewLC();
       
  1235     TInt err(KErrNone);
       
  1236    	err = ConvertSyncItemToNoteL(*buf, *note);       
       
  1237     if(err != KErrNone)
       
  1238     {
       
  1239     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: conversion error");
       
  1240     }
       
  1241     else
       
  1242     {
       
  1243 	    _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: before adding to notepad db");
       
  1244 	    TRAP(err,iNpdDb->AddNoteL(*note,lKey));
       
  1245 		if(err!=KErrNone)
       
  1246 		{
       
  1247 		    	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: Addnote error");	
       
  1248 		}
       
  1249 		else
       
  1250 		{
       
  1251 		    	TNSmlSnapshotItem item( lKey );
       
  1252 			    TTime lLastModified = note->LastModified();
       
  1253 				item.SetLastChangedDate(lLastModified);
       
  1254 				item.SetSoftDelete( EFalse );
       
  1255 				iChangeFinder->ItemAddedL( item ); 
       
  1256 		}
       
  1257     }
       
  1258     aUid = lKey;
       
  1259     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: after adding to notepad db");
       
  1260 	CleanupStack::PopAndDestroy(note);//note
       
  1261 	CleanupStack::PopAndDestroy(buf);//buf
       
  1262 	return err;
       
  1263 }
       
  1264 // ----------------------------------------------------------------------------
       
  1265 // void CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL(CBufBase& aItem, TInt& aUid)
       
  1266 // ----------------------------------------------------------------------------
       
  1267 TInt CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL(CBufBase& aItem, TInt& aUid)
       
  1268 {
       
  1269 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: begin");
       
  1270 	TInt length = aItem.Size();
       
  1271     HBufC8* buf = HBufC8::NewLC(length*KUnicodeSize + KNullCharLen);
       
  1272     buf->Des().Copy(aItem.Ptr(0));
       
  1273     TPtr8 lPtr(buf->Des());
       
  1274     CNpdItem *note;
       
  1275     note = CNpdItem::NewLC();
       
  1276     TInt err(KErrNone);
       
  1277     err = ConvertSyncItemToNoteL(*buf, *note);       
       
  1278     if(err != KErrNone)
       
  1279     {
       
  1280     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: conversion error");
       
  1281     }
       
  1282     else
       
  1283     {
       
  1284     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: before updating into notepad db");
       
  1285     	TRAP(err,iNpdDb->UpdateNoteL(aUid,*note));
       
  1286     
       
  1287     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: after updating into notepad db");
       
  1288 		if(err!=KErrNone)
       
  1289 		{
       
  1290     		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: updating error");	
       
  1291     	}
       
  1292     	else
       
  1293     	{
       
  1294     		TNSmlSnapshotItem item( aUid );
       
  1295 			item.SetLastChangedDate( note->LastModified() );
       
  1296 			item.SetSoftDelete( EFalse );
       
  1297 			iChangeFinder->ItemUpdatedL( item );
       
  1298 		}
       
  1299     }
       
  1300     CleanupStack::PopAndDestroy(note);//note
       
  1301     CleanupStack::PopAndDestroy(buf); //buf
       
  1302 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: end");
       
  1303 	return err;
       
  1304 	
       
  1305 }
       
  1306 // ------------------------------------------------------------------------------------------------
       
  1307 // CNSmlNotepadDataStore::DriveBelowCriticalLevelL()
       
  1308 // ------------------------------------------------------------------------------------------------
       
  1309 TBool CNSmlNotepadDataStore::DriveBelowCriticalLevelL( TInt aSize )
       
  1310 	{
       
  1311 		return SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive );
       
  1312 	}
       
  1313 
       
  1314 // End of File