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