omads/omadsextensions/adapters/agenda/src/nsmlagendadatastore.cpp
changeset 19 2691f6aa1921
parent 4 e6e896426eac
child 20 e1de7d03f843
equal deleted inserted replaced
4:e6e896426eac 19:2691f6aa1921
     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 agenda data store
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <ecom.h>
       
    22 #include <barsc.h>
       
    23 #include <bautils.h>
       
    24 #include <calsession.h> 
       
    25 #include <caldataexchange.h> 
       
    26 #include <calentryview.h>
       
    27 #include <caliterator.h>
       
    28 #include <calentry.h>
       
    29 #include <caldataformat.h>
       
    30 #include <caltime.h>
       
    31 #include <nsmldsimpluids.h>
       
    32 #include <sysutil.h>
       
    33 #include <SmlDataProvider.h>
       
    34 #include <SmlDataFormat.h>
       
    35 #include <SmlDataSyncDefs.h>
       
    36 #include <data_caging_path_literals.hrh>
       
    37 #include <nsmlagendadatastore_1_1_2.rsg>
       
    38 #include <e32property.h>
       
    39 #include <DataSyncInternalPSKeys.h>
       
    40 #include <CalenImporter.h>
       
    41 #include <CalenInterimUtils2.h>
       
    42 #include <utf.h>
       
    43 #include <nsmldebug.h>
       
    44 #include <nsmlagendaadapterhandler.h>
       
    45 #include <calenmulticaluids.hrh>
       
    46 #include <calenmulticalutil.h>
       
    47 #include <nsmlconstants.h>
       
    48 #include <centralrepository.h> 
       
    49 #include "nsmlagendadebug.h"
       
    50 #include "nsmlagendadatastore.h"
       
    51 #include "nsmlagendadataprovider.h"
       
    52 #include "nsmlchangefinder.h"
       
    53 #include "NSmlDataModBase.h"
       
    54 #include "nsmlagendadefines.hrh"
       
    55 
       
    56 #ifndef __WINS__
       
    57 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    58 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    59 // DBG_ARGS8 macro in no-debug builds.
       
    60 #pragma diag_remark 174
       
    61 #endif
       
    62 // ============================= MEMBER FUNCTIONS ==============================
       
    63 // -----------------------------------------------------------------------------
       
    64 // CNSmlAgendaDataStore::CNSmlAgendaDataStore
       
    65 // C++ default constructor can NOT contain any code, that
       
    66 // might leave.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CNSmlAgendaDataStore::CNSmlAgendaDataStore() :
       
    70 	iKey( TKeyArrayFix( _FOFF( TNSmlSnapshotItem, ItemId() ), ECmpTInt ) ),
       
    71 	iPos( -1 ),
       
    72 	iHasHistory( EFalse ),
       
    73 	iModificationCount( KNSmlCompactAfterChanges ),
       
    74 	iState( ENSmlClosed ),
       
    75 	iSnapshotRegistered( EFalse ),
       
    76 	iDrive( -1 ),
       
    77 	iReplaceItemId( -1 ),
       
    78 	iRXEntryType( ENSmlNotSet ),
       
    79 	iTXEntryType( ENSmlNotSet ),
       
    80 	iIsHierarchicalSyncSupported( EFalse ),
       
    81 	iParentItemId( 0 )
       
    82 	{
       
    83 	FLOG(_L("CNSmlAgendaDataStore::CNSmlAgendaDataStore(): BEGIN"));
       
    84 	// RD_MULTICAL
       
    85 	iDataMimeType = ENSmlNone;
       
    86 	iAgendaAdapterHandler = NULL;
       
    87 	// RD_MULTICAL
       
    88 	FLOG(_L("CNSmlAgendaDataStore::CNSmlAgendaDataStore(): END"));
       
    89 	}
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CNSmlAgendaDataStore::ConstructL
       
    93 // Symbian 2nd phase constructor can leave.
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CNSmlAgendaDataStore::ConstructL()
       
    97 	{
       
    98 	FLOG(_L("CNSmlAgendaDataStore::ConstructL: BEGIN"));
       
    99 
       
   100 	iStringPool.OpenL();
       
   101 	User::LeaveIfError( iRfs.Connect() );
       
   102 
       
   103 	iDataMod = new ( ELeave ) CNSmlVCalMod();
       
   104 	
       
   105 	iNewUids = new ( ELeave ) CNSmlDataItemUidSet();
       
   106 	iDeletedUids = new ( ELeave ) CNSmlDataItemUidSet();
       
   107 	iSoftDeletedUids = new ( ELeave ) CNSmlDataItemUidSet();
       
   108 	iMovedUids = new ( ELeave ) CNSmlDataItemUidSet();
       
   109 	iReplacedUids = new ( ELeave ) CNSmlDataItemUidSet();
       
   110 	
       
   111 	iDefaultStoreFileName = HBufC::NewL( KNSmlDefaultStoreNameMaxSize );
       
   112 	*iDefaultStoreFileName = KNSmlAgendaFileNameForDefaultDB; 
       
   113 	
       
   114 	
       
   115 	// RD_MULTICAL
       
   116 	iAgendaAdapterLog = NULL;
       
   117 	iCalOffsetArr = new (ELeave) CArrayFixFlat<TUint>( KArrayGranularity );
       
   118 	iCommittedUidArr = new ( ELeave ) CNSmlDataItemUidSet();
       
   119 	//iCalOffsetVal = CCalenMultiCalUtil::GetOffsetL();
       
   120 	// TODO: Tweak code: BEGIN
       
   121 	iCalOffsetVal = 100000;	
       
   122 	ListAllAgendaPluginAdaptersL();
       
   123 	// END  
       
   124 		
       
   125 	iOwnFormat = DoOwnStoreFormatL();
       
   126 	// RD_MULTICAL
       
   127 	iDataMod->SetOwnStoreFormat( *iOwnFormat );	
       
   128     iInterimUtils = CCalenInterimUtils2::NewL();
       
   129     
       
   130     // Initialize the FieldId to a default value [UID3 of the application]
       
   131     // This value will be rewritten once the exact single CalendarDB is determined
       
   132     // In case of HF synchronization involving multiple CalendarDB's the below value 
       
   133     // will be retained.
       
   134     iOpenedStoreId = KNSmlAgendaAdapterUid;
       
   135     
       
   136 	FLOG(_L("CNSmlAgendaDataStore::ConstructL: END"));
       
   137 	}
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CNSmlAgendaDataStore::NewL
       
   141 // Two-phased constructor.
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 CNSmlAgendaDataStore* CNSmlAgendaDataStore::NewL()
       
   145 	{
       
   146 	FLOG(_L("CNSmlAgendaDataStore::NewL: BEGIN"));
       
   147 	
       
   148 	CNSmlAgendaDataStore* self = new ( ELeave ) CNSmlAgendaDataStore();
       
   149 	CleanupStack::PushL( self );
       
   150 	
       
   151 	self->ConstructL();
       
   152 	CleanupStack::Pop(); // self
       
   153 	
       
   154 	FLOG(_L("CNSmlAgendaDataStore::NewL: END"));
       
   155 	return self;
       
   156 	}
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CNSmlAgendaDataStore::~CNSmlAgendaDataStore
       
   160 // Destructor.
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 CNSmlAgendaDataStore::~CNSmlAgendaDataStore()
       
   164 	{
       
   165 	FLOG(_L("CNSmlAgendaDataStore::~CNSmlAgendaDataStore(): BEGIN"));
       
   166 
       
   167     // Enable notifications
       
   168     TInt error( KErrNone );
       
   169    
       
   170     // TODO:
       
   171     /*if ( iVCalSession )
       
   172         {
       
   173         TRAP_IGNORE( iVCalSession->EnablePubSubNotificationsL() );
       
   174         TRAP_IGNORE( iVCalSession->EnableChangeBroadcast() );
       
   175         }*/  
       
   176 	
       
   177     if ( iChangeFinder )
       
   178 		{
       
   179 		TRAP( error, iChangeFinder->CloseL() );
       
   180 		}
       
   181 
       
   182     delete iChangeFinder;
       
   183 	delete iNewUids;
       
   184 	delete iDeletedUids;
       
   185 	delete iSoftDeletedUids;
       
   186 	delete iMovedUids;
       
   187 	delete iReplacedUids;
       
   188 
       
   189     delete iDefaultStoreFileName;
       
   190     delete iOpenedStoreName;
       
   191     delete iReceivedStoreName;
       
   192 	delete iDataMod;
       
   193 	delete iItemData;
       
   194 	
       
   195     delete iInterimUtils;
       
   196     
       
   197     iAgendaPluginAdapters.ResetAndDestroy();    
       
   198     
       
   199     if( iOwnFormat )
       
   200        {
       
   201        delete iOwnFormat;
       
   202        iOwnFormat = NULL;
       
   203        }
       
   204    
       
   205     iRfs.Close();
       
   206     iStringPool.Close();
       
   207     
       
   208     if ( iCalOffsetArr )
       
   209        {
       
   210        iCalOffsetArr->Reset();
       
   211        delete iCalOffsetArr;
       
   212        }
       
   213     
       
   214     if ( iCommittedUidArr )
       
   215        {
       
   216        iCommittedUidArr->Reset();
       
   217        delete iCommittedUidArr;
       
   218        }
       
   219 	FLOG(_L("CNSmlAgendaDataStore::~CNSmlAgendaDataStore(): END"));
       
   220 	}
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CNSmlAgendaDataStore::DoOpenL
       
   224 // Open calendar database for access.
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CNSmlAgendaDataStore::DoOpenL( const TDesC& aStoreName,
       
   228                 MSmlSyncRelationship& aContext, TRequestStatus& aStatus )
       
   229     {
       
   230 	FLOG(_L("CNSmlAgendaDataStore::DoOpenL: BEGIN"));
       
   231 	
       
   232 	iCallerStatus = &aStatus;
       
   233 	*iCallerStatus = KRequestPending;
       
   234 	if ( iState != ENSmlClosed )
       
   235 		{
       
   236 		User::RequestComplete( iCallerStatus, KErrInUse );
       
   237 		return;
       
   238 		}
       
   239 
       
   240 	if( RFs::CharToDrive( aStoreName[0], iDrive ) != KErrNone )
       
   241 		{
       
   242 		RFs::CharToDrive( KNSmlDriveC()[0], iDrive );
       
   243 		}
       
   244 	
       
   245 	if ( iReceivedStoreName )
       
   246         {
       
   247         delete iReceivedStoreName;
       
   248         iReceivedStoreName = NULL;
       
   249         } 
       
   250 	iReceivedStoreName = aStoreName.AllocL();
       
   251     // Open database
       
   252 	TInt err( KErrNone );	
       
   253 	if ( aStoreName == KNSmlAgendaFileNameForDefaultDB )
       
   254 		{
       
   255 		FLOG(_L("CNSmlAgendaDataStore::DoOpenL: Calling the OpenStoreL: '%S'"), &aStoreName);
       
   256 		TRAP( err, OpenStoreL() );
       
   257 		}
       
   258     else 
       
   259     	{
       
   260     	// Provided profile is created from the DS Application
       
   261     	FLOG(_L("CNSmlAgendaDataStore::DoOpenL: storename: '%S'"), &aStoreName);    	
       
   262     	if ( iOpenedStoreName )
       
   263             {
       
   264             delete iOpenedStoreName;
       
   265             iOpenedStoreName = NULL;
       
   266             }
       
   267         iOpenedStoreName = aStoreName.AllocL();  
       
   268         
       
   269         CCalSession* calsession = CCalSession::NewL();
       
   270         CleanupStack::PushL(calsession);
       
   271         TRAP( err, calsession->OpenL( aStoreName ) );
       
   272         if ( err )
       
   273             {
       
   274             FLOG(_L("CNSmlAgendaDataStore::DoOpenL: Cannot open the session: '%d'"), err); 
       
   275             CleanupStack::PopAndDestroy( calsession ); 
       
   276             User::RequestComplete( iCallerStatus, err );
       
   277             return;
       
   278             }
       
   279             
       
   280         // Disable notifications
       
   281         TRAP_IGNORE( calsession->DisablePubSubNotificationsL() );
       
   282         TRAP_IGNORE( calsession->DisableChangeBroadcast() );    
       
   283         // Get ID of database
       
   284         calsession->FileIdL( iOpenedStoreId );
       
   285         
       
   286         // Close the session
       
   287         CleanupStack::PopAndDestroy( calsession ); 
       
   288     	}
       
   289 		
       
   290 	if ( err )
       
   291 	    {
       
   292 	    User::RequestComplete( iCallerStatus, err );
       
   293 	    return;
       
   294 	    }
       
   295 	
       
   296 	if ( iChangeFinder )
       
   297 		{
       
   298 		iChangeFinder->CloseL();
       
   299 		delete iChangeFinder;
       
   300 		iChangeFinder = NULL;
       
   301 		}
       
   302 	
       
   303 	iChangeFinder = CNSmlChangeFinder::NewL( aContext, iKey, iHasHistory,
       
   304 	                                         KNSmlAgendaAdapterImplUid );
       
   305 	iAgendaAdapterLog = CNSmlAgendaAdapterLog::NewL( aContext );
       
   306 	iState = ENSmlOpenAndWaiting;
       
   307     User::RequestComplete( iCallerStatus, err );
       
   308 	    
       
   309 	FLOG(_L("CNSmlAgendaDataStore::DoOpenL: END"));
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CNSmlAgendaDataStore::DoCancelRequest
       
   314 // Set internal module state to previous state.
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 void CNSmlAgendaDataStore::DoCancelRequest()
       
   318     {
       
   319 	FLOG(_L("CNSmlAgendaDataStore::DoCancelRequest: BEGIN"));
       
   320     if ( iState == ENSmlOpenAndWaiting )
       
   321         {
       
   322     	iState = ENSmlClosed;
       
   323         }
       
   324     else
       
   325         {
       
   326 	    iState = ENSmlOpenAndWaiting;
       
   327         }
       
   328 	FLOG(_L("CNSmlAgendaDataStore::DoCancelRequest: END"));
       
   329     }
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CNSmlAgendaDataStore::DoStoreName
       
   333 // Returns previously opened database name.
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 const TDesC& CNSmlAgendaDataStore::DoStoreName() const
       
   337     {
       
   338 	FLOG(_L("CNSmlAgendaDataStore::DoStoreName: BEGIN"));
       
   339 	FLOG(_L("CNSmlAgendaDataStore::DoStoreName: END"));
       
   340 	return *iReceivedStoreName;
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CNSmlAgendaDataStore::DoBeginTransactionL
       
   345 // Not supported.
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 void CNSmlAgendaDataStore::DoBeginTransactionL()
       
   349     {
       
   350 	FLOG(_L("CNSmlAgendaDataStore::DoBeginTransactionL: BEGIN"));
       
   351 	User::Leave( KErrNotSupported );
       
   352 	FLOG(_L("CNSmlAgendaDataStore::DoBeginTransactionL: END"));
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CNSmlAgendaDataStore::DoCommitTransactionL
       
   357 // Not supported.
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 void CNSmlAgendaDataStore::DoCommitTransactionL( TRequestStatus& aStatus )
       
   361     {
       
   362 	FLOG(_L("CNSmlAgendaDataStore::DoCommitTransactionL: BEGIN"));
       
   363 	iCallerStatus = &aStatus;
       
   364 	*iCallerStatus = KRequestPending;
       
   365 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   366 	FLOG(_L("CNSmlAgendaDataStore::DoCommitTransactionL: END"));
       
   367     }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CNSmlAgendaDataStore::DoRevertTransaction
       
   371 // Not supported.
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CNSmlAgendaDataStore::DoRevertTransaction( TRequestStatus& aStatus )
       
   375     {
       
   376 	FLOG(_L("CNSmlAgendaDataStore::DoRevertTransaction: BEGIN"));
       
   377 	iCallerStatus = &aStatus;
       
   378 	*iCallerStatus = KRequestPending;
       
   379 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   380 	FLOG(_L("CNSmlAgendaDataStore::DoRevertTransaction: END"));
       
   381     }
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // CNSmlAgendaDataStore::DoBeginBatchL
       
   385 // Not supported.
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 void CNSmlAgendaDataStore::DoBeginBatchL()
       
   389     {
       
   390 	FLOG(_L("CNSmlAgendaDataStore::DoBeginBatchL: BEGIN"));
       
   391 	User::Leave( KErrNotSupported );
       
   392 	FLOG(_L("CNSmlAgendaDataStore::DoBeginBatchL: END"));
       
   393     }
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CNSmlAgendaDataStore::DoCommitBatchL
       
   397 // Not supported.
       
   398 // -----------------------------------------------------------------------------
       
   399 //
       
   400 void CNSmlAgendaDataStore::DoCommitBatchL( RArray<TInt>& /*aResultArray*/,
       
   401                 TRequestStatus& aStatus )
       
   402     {
       
   403 	FLOG(_L("CNSmlAgendaDataStore::DoCommitBatchL: BEGIN"));
       
   404 	iCallerStatus = &aStatus;
       
   405 	*iCallerStatus = KRequestPending;
       
   406 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   407 	FLOG(_L("CNSmlAgendaDataStore::DoCommitBatchL: END"));
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // CNSmlAgendaDataStore::DoCancelBatch
       
   412 // Not supported.
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 void CNSmlAgendaDataStore::DoCancelBatch()
       
   416     {
       
   417 	FLOG(_L("CNSmlAgendaDataStore::DoCancelBatch: BEGIN"));
       
   418 	// Nothing to do
       
   419 	FLOG(_L("CNSmlAgendaDataStore::DoCancelBatch: END"));
       
   420     }
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CNSmlAgendaDataStore::DoSetRemoteStoreFormatL
       
   424 // Set SyncML Remote Server data store format.
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 void CNSmlAgendaDataStore::DoSetRemoteStoreFormatL(
       
   428                 const CSmlDataStoreFormat& aServerDataStoreFormat )
       
   429     {
       
   430 	FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: BEGIN"));
       
   431 	
       
   432 	if ( iOwnFormat )
       
   433 	    {
       
   434 	    delete iOwnFormat;
       
   435 	    iOwnFormat = NULL;    
       
   436 	    }
       
   437 	
       
   438 	iOwnFormat = DoOwnStoreFormatL();
       
   439 	// RD_MULTICAL
       
   440 	if ( aServerDataStoreFormat.IsSupported( 
       
   441 	                                CSmlDataStoreFormat::EOptionHierarchial ) )
       
   442         {
       
   443         FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: Supports HIERARCHIAL"));
       
   444        
       
   445 		
       
   446         iIsHierarchicalSyncSupported = ETrue;
       
   447 	
       
   448 		// Check what properties are supported for folders
       
   449         iAgendaAdapterHandler->CheckServerSupportForFolder( aServerDataStoreFormat );
       
   450         }
       
   451 	else
       
   452 	    {
       
   453 	    FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: Do not support HIERARCHIAL"));	           
       
   454 	    iIsHierarchicalSyncSupported = EFalse;
       
   455 	    }
       
   456 	// RD_MULTICAL       
       
   457 	iDataMod->SetOwnStoreFormat( *iOwnFormat );
       
   458 	
       
   459     iDataMod->SetPartnerStoreFormat( ( CSmlDataStoreFormat& )
       
   460                 aServerDataStoreFormat );
       
   461     
       
   462     // Check which calendar type (vCal/iCal) is used
       
   463     // If server supports iCal then it is used
       
   464     // Otherwise vCal is used
       
   465     TInt returnValue( KErrNotSupported );
       
   466 
       
   467 #ifdef __NSML_USE_ICAL_FEATURE
       
   468 
       
   469     FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: Support iCal"));
       
   470    	returnValue = iDataMod->SetUsedMimeType(
       
   471        	    iOwnFormat->MimeFormat( 1 ).MimeType(),
       
   472            	iOwnFormat->MimeFormat( 1 ).MimeVersion() );
       
   473 
       
   474 #endif // __NSML_USE_ICAL_FEATURE
       
   475 
       
   476     if ( returnValue == KErrNone )
       
   477         {
       
   478         FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: Sets iCal"));
       
   479         iRXEntryType = ENSmlICal;
       
   480         iTXEntryType = ENSmlICal;
       
   481         }
       
   482     else
       
   483         {
       
   484         FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: Support vCal"));
       
   485         returnValue = iDataMod->SetUsedMimeType(
       
   486             iOwnFormat->MimeFormat( 0 ).MimeType(),
       
   487             iOwnFormat->MimeFormat( 0 ).MimeVersion() );
       
   488         if ( returnValue == KErrNone )
       
   489             {
       
   490             FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: Sets vCal"));
       
   491             iRXEntryType = ENSmlVCal;
       
   492             iTXEntryType = ENSmlVCal;
       
   493             }
       
   494         }
       
   495     if ( iRXEntryType == ENSmlNotSet || iTXEntryType == ENSmlNotSet )
       
   496         {
       
   497         // Leave if server does not support either vCal or iCal
       
   498         FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: MimeType Not supported"));
       
   499         User::Leave( KErrNotFound );        
       
   500         }
       
   501     
       
   502     // RD_MULTICAL
       
   503     if( !iSnapshotRegistered )
       
   504         {
       
   505         RegisterSnapshotL();
       
   506         }
       
   507     // RD_MULTICAL
       
   508     
       
   509 	FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteStoreFormatL: END"));
       
   510     }
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // CNSmlAgendaDataStore::DoSetRemoteMaxObjectSize
       
   514 // Set SyncML Remote Server maximum object size.
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 void CNSmlAgendaDataStore::DoSetRemoteMaxObjectSize( TInt aServerMaxObjectSize )
       
   518     {
       
   519 	FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteMaxObjectSize: BEGIN"));
       
   520 	iServerMaxObjectSize = aServerMaxObjectSize;
       
   521 	FLOG(_L("CNSmlAgendaDataStore::DoSetRemoteMaxObjectSize: END"));
       
   522     }
       
   523 
       
   524 // -----------------------------------------------------------------------------
       
   525 // CNSmlAgendaDataStore::DoMaxObjectSize
       
   526 // Set SyncML Component maximum object size.
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 TInt CNSmlAgendaDataStore::DoMaxObjectSize() const
       
   530     {
       
   531 	FLOG(_L("CNSmlAgendaDataStore::DoMaxObjectSize: BEGIN"));
       
   532 	FLOG(_L("CNSmlAgendaDataStore::DoMaxObjectSize - Default: END"));
       
   533 	return KNSmlAgendaOwnMaxObjectSize;
       
   534     }
       
   535 
       
   536 // -----------------------------------------------------------------------------
       
   537 // CNSmlAgendaDataStore::DoOpenItemL
       
   538 // Open calendar item for reading.
       
   539 // -----------------------------------------------------------------------------
       
   540 //
       
   541 void CNSmlAgendaDataStore::DoOpenItemL( TSmlDbItemUid aUid,
       
   542                                         TBool& aFieldChange,
       
   543                                         TInt& aSize,
       
   544                                         TSmlDbItemUid& aParent,
       
   545                                         TDes8& aMimeType,
       
   546                                         TDes8& aMimeVer,
       
   547                                         TRequestStatus& aStatus )
       
   548     {
       
   549 	FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: BEGIN"));
       
   550     iCallerStatus = &aStatus;
       
   551 	*iCallerStatus = KRequestPending;
       
   552 	// Leave if Data Store is in wrong state
       
   553 	if ( iState != ENSmlOpenAndWaiting )
       
   554 		{
       
   555 		FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL:Returing due to invalid state"));
       
   556 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   557 		return;
       
   558 		}
       
   559 		
       
   560 	// Add field change info
       
   561 	aFieldChange = EFalse;
       
   562 	
       
   563 	// Update the SnapShots
       
   564 	if ( !iSnapshotRegistered )
       
   565         {
       
   566         RegisterSnapshotL();
       
   567         }
       
   568         
       
   569 	// Initialize the Buffer
       
   570     delete iItemData;
       
   571     iItemData = NULL;
       
   572     iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   573 
       
   574     RBufWriteStream writeStream( *iItemData );
       
   575     writeStream.PushL();
       
   576 
       
   577 	// RD_MULTICAL
       
   578   	//Determine the Mime Type
       
   579     DataMimeType( aUid );
       
   580 	TInt fetchError( KErrNone );
       
   581 	switch( iDataMimeType )
       
   582 	    {
       
   583 	    case ENSmlFolder:
       
   584     	    {
       
   585     	    //Set the Parent UID
       
   586     	    aParent = KDbItemUidRoot; // parent is root
       
   587     	    FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: calling fetch"));
       
   588 	        FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL:writestream before size: '%d'"), writeStream.Sink()->SizeL());
       
   589 	        TRAP( fetchError, iAgendaAdapterHandler->FetchFolderL( aUid, writeStream ) );
       
   590 	        FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL:writestream size: '%d'"), writeStream.Sink()->SizeL());
       
   591     	
       
   592 	        if(fetchError != KErrNone)
       
   593     	        {
       
   594     	        FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: fetch error: '%d'"), fetchError);
       
   595     	        CleanupStack::PopAndDestroy( ); // writeStream
       
   596     	        User::RequestComplete( iCallerStatus, fetchError );
       
   597     	        return;
       
   598     	        }
       
   599     	    
       
   600             aMimeType.Append( KNSmlContentTypeFolder );
       
   601             aMimeVer.Append( KNSmlContentTypeFolderVersion );
       
   602             
       
   603             writeStream.CommitL();
       
   604             iItemData->Compress();
       
   605             iPos = 0;
       
   606             }
       
   607     	    break;
       
   608 	    case ENSmlCalendar:
       
   609     	    {
       
   610     	    FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: calendar item"));
       
   611             CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
   612             HBufC* calfilename = NULL;
       
   613             TCalLocalUid parentid(NULL);
       
   614             TCalLocalUid entryid(aUid);
       
   615             TInt err( KErrNone );
       
   616             
       
   617             if( iIsHierarchicalSyncSupported )
       
   618                 {
       
   619                 TRAP( err, GetCalendarEntryIdL( parentid, entryid ));
       
   620                 if ( err )
       
   621                     {
       
   622                     FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL:Parent Id is not Valid one"));
       
   623                     User::RequestComplete( iCallerStatus, KErrNotFound );
       
   624                     return;
       
   625                     }
       
   626                 aParent = parentid;
       
   627                 //Get the Folder Name
       
   628                 TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL( parentid ));  
       
   629                 }
       
   630             else
       
   631                 {
       
   632                 calfilename = iOpenedStoreName->AllocL();
       
   633                 }
       
   634             
       
   635             if( err != KErrNone || NULL == calfilename )
       
   636                 {
       
   637                 FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL:Invalid CalendarInfo"));
       
   638                 User::RequestComplete( iCallerStatus, KErrNotFound );
       
   639                 return;
       
   640                 }     
       
   641             
       
   642             iReplaceItemId = entryid;            
       
   643             agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
   644             if( agendautil )
       
   645                 {
       
   646                 CleanupStack::PushL(agendautil);
       
   647                 TRAP(err, agendautil->InitializeCalAPIsL( calfilename, entryid ));
       
   648                 CleanupStack::Pop(agendautil);
       
   649                 }
       
   650             if ( err || !agendautil )
       
   651                 {
       
   652                 FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: entry is not valid"));
       
   653                 delete agendautil; 
       
   654                 delete calfilename;
       
   655                 User::RequestComplete( iCallerStatus, KErrNotFound );
       
   656                 return;
       
   657                 }    
       
   658             // Export item from database
       
   659             if ( iTXEntryType == ENSmlICal )
       
   660                 {
       
   661                 FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: Export - iCal DB"));
       
   662                 agendautil->iExporter->ExportICalL( *agendautil->iEntry, writeStream );
       
   663                 aMimeType = iOwnFormat->MimeFormat( 1 ).MimeType().DesC();
       
   664                 aMimeVer = iOwnFormat->MimeFormat( 1 ).MimeVersion().DesC();
       
   665                 }
       
   666             else if ( iTXEntryType == ENSmlVCal )
       
   667                 {
       
   668                 FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: Export - vCal DB"));
       
   669                 agendautil->iExporter->ExportVCalL( *agendautil->iEntry, writeStream );
       
   670                 aMimeType = iOwnFormat->MimeFormat( 0 ).MimeType().DesC();
       
   671                 aMimeVer = iOwnFormat->MimeFormat( 0 ).MimeVersion().DesC();
       
   672                 }
       
   673             else
       
   674                 {
       
   675                 FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: Export - DB Not Supported"));
       
   676                 delete agendautil;
       
   677                 delete calfilename;
       
   678                 CleanupStack::PopAndDestroy( &writeStream ); 
       
   679                 User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   680                 return;
       
   681                 }
       
   682             writeStream.CommitL();
       
   683             iItemData->Compress();
       
   684             iPos = 0;
       
   685      
       
   686             iDataMod->StripTxL( *iItemData );
       
   687             delete agendautil;
       
   688             delete calfilename;
       
   689             }
       
   690     	    break;
       
   691 	    default:
       
   692     	    {
       
   693     	    iDataMimeType = ENSmlNone;
       
   694     	    CleanupStack::PopAndDestroy( ); // writeStream
       
   695     	    User::RequestComplete( iCallerStatus, KErrNotFound );
       
   696     	    return;
       
   697     	    }
       
   698 	    }
       
   699     
       
   700 	CleanupStack::PopAndDestroy( ); // writeStream
       
   701 	
       
   702 	FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: destroying the stream"));
       
   703 	
       
   704 	// Set the Size
       
   705 	aSize = iItemData->Size();
       
   706 	
       
   707 	iState = ENSmlItemOpen;
       
   708 	
       
   709 	FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: user complete"));
       
   710 	
       
   711 	if ( iServerMaxObjectSize == 0 || aSize <= iServerMaxObjectSize )
       
   712 		{
       
   713 		FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: error none"));
       
   714 		User::RequestComplete( iCallerStatus, KErrNone );
       
   715 		}
       
   716 	else
       
   717 		{
       
   718 		FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: error too big"));
       
   719 		User::RequestComplete( iCallerStatus, KErrTooBig );
       
   720 		}
       
   721 	FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: END"));
       
   722     }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CNSmlAgendaDataStore::DoCreateItemL
       
   726 // Prepare item data for writing to database. WriteItemL() writes item's data as
       
   727 // buffered.
       
   728 // -----------------------------------------------------------------------------
       
   729 //
       
   730 void CNSmlAgendaDataStore::DoCreateItemL( TSmlDbItemUid& aUid,
       
   731                                           TInt aSize,
       
   732                                           TSmlDbItemUid aParent,
       
   733                                           const TDesC8& aMimeType,
       
   734                                           const TDesC8& /*aMimeVer*/,
       
   735                                           TRequestStatus& aStatus )
       
   736     {
       
   737 	FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: BEGIN"));
       
   738 	FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: Parent id: '%d'"), aParent);
       
   739 	FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: UID: '%d'"), aUid);
       
   740 	iCallerStatus = &aStatus;
       
   741 	*iCallerStatus = KRequestPending;
       
   742 	iAddItemId = &aUid;
       
   743 	
       
   744     //Leave if Data Store is in wrong state
       
   745     if ( iState != ENSmlOpenAndWaiting )
       
   746         {
       
   747         User::RequestComplete( iCallerStatus, KErrNotReady );
       
   748         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL - KErrNotReady: END"));
       
   749         return;
       
   750         }
       
   751 
       
   752     // Leave if item is larger than we support
       
   753     if ( KNSmlAgendaOwnMaxObjectSize < aSize )
       
   754         {
       
   755         User::RequestComplete( iCallerStatus, KErrTooBig );
       
   756         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL - KErrTooBig: END"));
       
   757         return;
       
   758         }
       
   759         
       
   760     // Check the drive free space
       
   761     if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive ) )
       
   762         {
       
   763         User::RequestComplete( iCallerStatus, KErrDiskFull );
       
   764         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL - KErrDiskFull: END"));
       
   765         return;
       
   766         }
       
   767     
       
   768     // Check the MIME type
       
   769     if ( 0 == iOwnFormat->MimeFormat( 0 ).MimeType().DesC().Compare( aMimeType ) )
       
   770         {
       
   771         // vcal
       
   772         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: received vCal"));
       
   773         iRXEntryType = ENSmlVCal;
       
   774         iDataMimeType = ENSmlCalendar;
       
   775         
       
   776         TInt err( KErrNone );
       
   777         // Check the Sync Status
       
   778         if( iIsHierarchicalSyncSupported )
       
   779             {
       
   780             if( !iAgendaAdapterHandler->FolderSyncStatusL( aParent ) )
       
   781                 {
       
   782                 User::RequestComplete( iCallerStatus, KErrGeneral );
       
   783                 FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL - Sync Disabled: END"));
       
   784                 return;
       
   785                 }
       
   786             }
       
   787         else
       
   788             {
       
   789             TBool syncstatus( EFalse );
       
   790             HBufC* calfilename = iOpenedStoreName->AllocL();
       
   791             TRAP( err, syncstatus = iAgendaAdapterHandler->FolderSyncStatusL( calfilename ) );
       
   792             if( err || !syncstatus )
       
   793                 {
       
   794                 delete calfilename;
       
   795                 User::RequestComplete( iCallerStatus, KErrGeneral );
       
   796                 FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL - Sync Disabled: END"));
       
   797                 return;
       
   798                 }
       
   799             delete calfilename;            
       
   800             }
       
   801         }
       
   802 #ifdef __NSML_USE_ICAL_FEATURE     
       
   803     else if ( 0 == iOwnFormat->MimeFormat( 1 ).MimeType().DesC().Compare( aMimeType ) ) 
       
   804         {
       
   805         //ical
       
   806         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: received iCal"));
       
   807         iRXEntryType = ENSmlICal;
       
   808         iDataMimeType = ENSmlCalendar;
       
   809         }
       
   810 #endif // __NSML_USE_ICAL_FEATURE
       
   811     // RD_MULTICAL
       
   812     else if( aMimeType == KNSmlContentTypeFolder() && iIsHierarchicalSyncSupported )
       
   813         {
       
   814         // Check the validity 
       
   815         /*if ( aUid <= 0 || aParent <= 0 )
       
   816           {
       
   817           FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: Corrupt ids"));
       
   818           User::RequestComplete( iCallerStatus, KErrCorrupt );
       
   819           return;
       
   820           }*/
       
   821         iDataMimeType = ENSmlFolder;
       
   822         }
       
   823     // RD_MULTICAL
       
   824     else
       
   825         {
       
   826         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: \
       
   827                                mime type not received"));
       
   828         iRXEntryType = iTXEntryType;
       
   829         iDataMimeType = ENSmlNone;
       
   830         User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   831         FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: end with leave"));
       
   832         return;
       
   833         }
       
   834     
       
   835 	if( iIsHierarchicalSyncSupported )
       
   836 		{	
       
   837 	    // Check that parent exists
       
   838 	    iParentItemId = aParent;
       
   839 	    if ( iParentItemId == KDbItemUidRoot 
       
   840 	         && iDataMimeType == ENSmlCalendar )
       
   841 	        {
       
   842 	        FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: ParentId is NULL"));
       
   843 	        iParentItemId = NULL;
       
   844 	        User::RequestComplete( iCallerStatus, KErrGeneral );
       
   845 	        return;
       
   846 	        }
       
   847 	  
       
   848 	    TInt index = KErrNotFound;
       
   849 	    TInt err = KErrNone;
       
   850 	    TKeyArrayFix key( 0, ECmpTInt ); // Find key for Ids.
       
   851 	    
       
   852 	    err = iCalOffsetArr->Find( iParentItemId, key, index );
       
   853 	 
       
   854 	    if( err != KErrNone && iDataMimeType == ENSmlCalendar )
       
   855 	        {
       
   856 	        FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: ParentId is NotFound"));
       
   857 	        iParentItemId = NULL;
       
   858 	        User::RequestComplete( iCallerStatus, KErrNotFound );
       
   859 	        return;
       
   860 	        }
       
   861 		}
       
   862     
       
   863     // TODO: Any other validation is required ?
       
   864     
       
   865     // Register snapshots if needed
       
   866     if( !iSnapshotRegistered )
       
   867         {
       
   868         RegisterSnapshotL();
       
   869         }
       
   870     
       
   871     // Buffer is reinitialized
       
   872     delete iItemData;
       
   873     iItemData = NULL;
       
   874     iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   875     iPos = 0;
       
   876     
       
   877     iState = ENSmlItemCreating;
       
   878     User::RequestComplete( iCallerStatus, KErrNone );
       
   879 
       
   880 	FLOG(_L("CNSmlAgendaDataStore::DoCreateItemL: END"));
       
   881     }
       
   882 
       
   883 // -----------------------------------------------------------------------------
       
   884 // CNSmlAgendaDataStore::DoReplaceItemL
       
   885 // Prepare item data for writing and replacing item in database. WriteItemL()
       
   886 // writes item's data as buffered.
       
   887 // -----------------------------------------------------------------------------
       
   888 //
       
   889 void CNSmlAgendaDataStore::DoReplaceItemL( TSmlDbItemUid aUid,
       
   890                                            TInt aSize,
       
   891                                            TSmlDbItemUid aParent,
       
   892                                            TBool aFieldChange,
       
   893                                            TRequestStatus& aStatus )
       
   894     {
       
   895 	FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: BEGIN"));
       
   896 	FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: Parent id: '%d'"), aParent);
       
   897 	FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: UID: '%d'"), aUid);
       
   898 	
       
   899 	iCallerStatus = &aStatus;
       
   900 	*iCallerStatus = KRequestPending;
       
   901 	TInt err( KErrNone );
       
   902 	
       
   903     // Leave if Data Store is in wrong state
       
   904     if ( iState != ENSmlOpenAndWaiting )
       
   905         {
       
   906         User::RequestComplete( iCallerStatus, KErrNotReady );
       
   907         FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - KErrNotReady: END"));
       
   908         return;
       
   909         }
       
   910 
       
   911     // Leave if item is larger than we support
       
   912     if ( KNSmlAgendaOwnMaxObjectSize < aSize )
       
   913         {
       
   914         User::RequestComplete( iCallerStatus, KErrTooBig );
       
   915         FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - KErrTooBig: END"));
       
   916         return;
       
   917         }
       
   918 
       
   919     // This adapter does not support Field Level sync
       
   920     if ( aFieldChange )
       
   921         {
       
   922         User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   923         FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL \
       
   924                    - KErrNotSupported: END"));
       
   925         return;
       
   926         }
       
   927     
       
   928     // Check the drive free space
       
   929     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive ) )
       
   930         {
       
   931         User::RequestComplete( iCallerStatus, KErrDiskFull );
       
   932         FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - KErrDiskFull: END"));
       
   933         return;
       
   934         }
       
   935     
       
   936     // Check the Sync Status
       
   937     if( iIsHierarchicalSyncSupported )
       
   938         {
       
   939         TCalLocalUid parentid( aParent );
       
   940         TCalLocalUid entryid( aUid );
       
   941         
       
   942         TRAP( err, GetCalendarEntryIdL( parentid, entryid ));
       
   943         if ( err )
       
   944             {
       
   945             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: Invalid UID"));
       
   946             User::RequestComplete( iCallerStatus, KErrGeneral );
       
   947             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - Sync Disabled: END"));
       
   948             return;
       
   949             }         
       
   950         
       
   951         if( !iAgendaAdapterHandler->FolderSyncStatusL( parentid ) )
       
   952             {
       
   953             User::RequestComplete( iCallerStatus, KErrGeneral );
       
   954             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - Sync Disabled: END"));
       
   955             return;
       
   956             }
       
   957         }
       
   958     else
       
   959         {
       
   960         TBool syncstatus( EFalse );
       
   961         HBufC* calfilename = iOpenedStoreName->AllocL();
       
   962         TRAP( err, syncstatus = iAgendaAdapterHandler->FolderSyncStatusL( calfilename ) );
       
   963         if( err || !syncstatus )
       
   964             {
       
   965             delete calfilename;
       
   966             User::RequestComplete( iCallerStatus, KErrGeneral );
       
   967             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - Sync Disabled: END"));
       
   968             return;
       
   969             }
       
   970         delete calfilename;            
       
   971         }
       
   972     
       
   973     // Determine the Mime Type
       
   974     DataMimeType( aUid ); 
       
   975     switch( iDataMimeType )
       
   976         {
       
   977         case ENSmlFolder:
       
   978             {
       
   979             iReplaceItemId = aUid;
       
   980             }
       
   981             break;
       
   982         case ENSmlCalendar:
       
   983             {
       
   984             CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
   985             HBufC* calfilename = NULL;
       
   986             iParentItemId = aParent;
       
   987             
       
   988             if( iIsHierarchicalSyncSupported )
       
   989                 {
       
   990                 iReplaceItemId = aUid - aParent;
       
   991                 //Get the Folder Information
       
   992                 TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL( aParent ));
       
   993                 }
       
   994             else
       
   995                 {
       
   996                 iReplaceItemId = aUid;
       
   997                 calfilename = iOpenedStoreName->AllocL();
       
   998                 }
       
   999                 
       
  1000             if( err != KErrNone || NULL == calfilename )
       
  1001                 {
       
  1002                 FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL:Invalid CalendarInfo"));
       
  1003                 User::RequestComplete( iCallerStatus, KErrNotFound );
       
  1004                 return;
       
  1005                 }
       
  1006             
       
  1007             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: entry id to be fetched: '%d'"), iReplaceItemId);             
       
  1008             agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  1009             if( agendautil )
       
  1010                 {
       
  1011                 CleanupStack::PushL(agendautil);
       
  1012                 TRAP(err, agendautil->InitializeCalAPIsL( calfilename, iReplaceItemId ));
       
  1013                 CleanupStack::Pop(agendautil);
       
  1014                 }
       
  1015                 
       
  1016             if ( err || !agendautil )
       
  1017                 {
       
  1018                 FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: entry is not valid"));  
       
  1019                 delete agendautil;
       
  1020                 delete calfilename;             
       
  1021                 User::RequestComplete( iCallerStatus, KErrNotFound );               
       
  1022                 return;
       
  1023                 }
       
  1024             delete agendautil;
       
  1025             delete calfilename;  
       
  1026             }
       
  1027             break;
       
  1028         default:
       
  1029             {
       
  1030             iDataMimeType = ENSmlNone;
       
  1031             User::RequestComplete( iCallerStatus, KErrNotSupported );
       
  1032             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL \
       
  1033                        - KErrNotSupported: END"));
       
  1034             return;
       
  1035             }
       
  1036         }
       
  1037     
       
  1038     if ( !iSnapshotRegistered )
       
  1039         {
       
  1040         RegisterSnapshotL();
       
  1041         }
       
  1042    
       
  1043     //Reinitialize the Buffer
       
  1044     delete iItemData;
       
  1045     iItemData = NULL;
       
  1046     iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
  1047     iPos = 0;
       
  1048     
       
  1049     iState = ENSmlItemUpdating;
       
  1050     User::RequestComplete( iCallerStatus, KErrNone );
       
  1051 	
       
  1052 	FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL: END"));
       
  1053     }
       
  1054 
       
  1055 // -----------------------------------------------------------------------------
       
  1056 // CNSmlAgendaDataStore::DoReadItemL
       
  1057 // Read item data to given buffer.
       
  1058 // -----------------------------------------------------------------------------
       
  1059 //
       
  1060 void CNSmlAgendaDataStore::DoReadItemL( TDes8& aBuffer )
       
  1061     {
       
  1062 	FLOG(_L("CNSmlAgendaDataStore::DoReadItemL: BEGIN"));
       
  1063     if ( iState != ENSmlItemOpen || !iItemData )
       
  1064         {
       
  1065         iPos = -1;
       
  1066         User::Leave( KErrNotReady );
       
  1067         }
       
  1068 
       
  1069     if ( iPos == -1 )
       
  1070         {
       
  1071         User::Leave( KErrEof );
       
  1072         }
       
  1073 
       
  1074     if ( aBuffer.Size() < iItemData->Size() - iPos )
       
  1075         {
       
  1076         iItemData->Read( iPos, aBuffer );
       
  1077         iPos += aBuffer.Size();
       
  1078         }
       
  1079     else
       
  1080         {
       
  1081         iItemData->Read( iPos, aBuffer, iItemData->Size() - iPos );
       
  1082         iPos = -1;
       
  1083         }
       
  1084 	FLOG(_L("CNSmlAgendaDataStore::DoReadItemL: END"));
       
  1085     }
       
  1086 
       
  1087 // -----------------------------------------------------------------------------
       
  1088 // CNSmlAgendaDataStore::DoWriteItemL
       
  1089 // Write item data as buffered.
       
  1090 // -----------------------------------------------------------------------------
       
  1091 //
       
  1092 void CNSmlAgendaDataStore::DoWriteItemL( const TDesC8& aData )
       
  1093     {
       
  1094 	FLOG(_L("CNSmlAgendaDataStore::DoWriteItemL: BEGIN"));
       
  1095 	if ( iState == ENSmlItemCreating || iState == ENSmlItemUpdating )
       
  1096 		{
       
  1097 		if ( iItemData )
       
  1098 			{
       
  1099 			if ( iPos == -1 )
       
  1100 				{
       
  1101 				User::Leave( KErrEof );
       
  1102 				}
       
  1103 			iItemData->InsertL( iPos, aData );
       
  1104 			iPos += aData.Size();
       
  1105 			return;
       
  1106 			}
       
  1107 		}
       
  1108 	User::Leave( KErrNotReady );
       
  1109 	FLOG(_L("CNSmlAgendaDataStore::DoWriteItemL: END"));
       
  1110     }
       
  1111 
       
  1112 // -----------------------------------------------------------------------------
       
  1113 // CNSmlAgendaDataStore::DoCommitItemL
       
  1114 // Commit item data to database when adding or replacing item.
       
  1115 // -----------------------------------------------------------------------------
       
  1116 //
       
  1117 void CNSmlAgendaDataStore::DoCommitItemL( TRequestStatus& aStatus )
       
  1118     {
       
  1119     FLOG(_L("CNSmlAgendaDataStore::DoCommitItemL: BEGIN"));
       
  1120     iCallerStatus = &aStatus;
       
  1121     *iCallerStatus = KRequestPending;
       
  1122     
       
  1123     // Leave if Data Store is in wrong state
       
  1124     if ( iState != ENSmlItemCreating && iState != ENSmlItemUpdating )
       
  1125         {
       
  1126         User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1127         FLOG(_L("CNSmlAgendaDataStore::DoCommitItemL - KErrNotReady: END"));
       
  1128         return;
       
  1129         }
       
  1130     
       
  1131     iItemData->Compress();
       
  1132     TInt error( KErrNone );
       
  1133     
       
  1134     // RD_MULTICAL
       
  1135     switch(iDataMimeType)
       
  1136         {
       
  1137         case ENSmlCalendar:
       
  1138             {
       
  1139             if( iState == ENSmlItemCreating )
       
  1140                 {
       
  1141                 TRAP( error, DoCommitCreateCalItemL() );
       
  1142                 }
       
  1143             else
       
  1144                 {
       
  1145                 TRAP( error, DoCommitReplaceCalItemL() );
       
  1146                 }
       
  1147             }
       
  1148             break;
       
  1149         case ENSmlFolder:
       
  1150             {
       
  1151             if( iState == ENSmlItemCreating )
       
  1152                 {
       
  1153                 TRAP( error, DoCommitCreateFolderItemL() );
       
  1154                 }
       
  1155             else
       
  1156                 {
       
  1157                 TRAP( error, DoCommitReplaceFolderItemL() );
       
  1158                 }
       
  1159             }
       
  1160             break;
       
  1161         default:
       
  1162             {
       
  1163             FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateItemL - \
       
  1164                                            KErrNotSupported: END"));
       
  1165             User::Leave( KErrNotSupported );
       
  1166             }                
       
  1167         }
       
  1168     // RD_MULTICAL
       
  1169     
       
  1170     iReplaceItemId = -1;
       
  1171     iPos = -1;
       
  1172     iState = ENSmlOpenAndWaiting;
       
  1173     iRXEntryType = iTXEntryType;
       
  1174     User::RequestComplete( iCallerStatus, error );    
       
  1175     FLOG(_L("CNSmlAgendaDataStore::DoCommitItemL: END"));
       
  1176     }
       
  1177 
       
  1178 // -----------------------------------------------------------------------------
       
  1179 // CNSmlAgendaDataStore::DoCloseItem
       
  1180 // Return to previous state and clean item buffer.
       
  1181 // -----------------------------------------------------------------------------
       
  1182 //
       
  1183 void CNSmlAgendaDataStore::DoCloseItem()
       
  1184     {
       
  1185 	FLOG(_L("CNSmlAgendaDataStore::DoCloseItem: BEGIN"));
       
  1186 	if ( iState == ENSmlItemOpen )
       
  1187 		{
       
  1188 		iPos = -1;
       
  1189 		iState = ENSmlOpenAndWaiting;
       
  1190 		}
       
  1191 	FLOG(_L("CNSmlAgendaDataStore::DoCloseItem: END"));
       
  1192     }
       
  1193 
       
  1194 // -----------------------------------------------------------------------------
       
  1195 // CNSmlAgendaDataStore::DoMoveItemL
       
  1196 // Not supported.
       
  1197 // -----------------------------------------------------------------------------
       
  1198 //
       
  1199 void CNSmlAgendaDataStore::DoMoveItemL( TSmlDbItemUid /*aUid*/,
       
  1200                                         TSmlDbItemUid /*aNewParent*/,
       
  1201                                         TRequestStatus& aStatus )
       
  1202     {
       
  1203 	FLOG(_L("CNSmlAgendaDataStore::DoMoveItemL: BEGIN"));
       
  1204 	
       
  1205     iCallerStatus = &aStatus;
       
  1206     *iCallerStatus = KRequestPending;
       
  1207     User::RequestComplete( iCallerStatus, KErrNotSupported );
       
  1208 	    
       
  1209 	FLOG(_L("CNSmlAgendaDataStore::DoMoveItemL: END"));
       
  1210     }
       
  1211 
       
  1212 // -----------------------------------------------------------------------------
       
  1213 // CNSmlAgendaDataStore::DoDeleteItemL
       
  1214 // Delete item from database.
       
  1215 // -----------------------------------------------------------------------------
       
  1216 //
       
  1217 void CNSmlAgendaDataStore::DoDeleteItemL( TSmlDbItemUid aUid,
       
  1218                                           TRequestStatus& aStatus )
       
  1219     {
       
  1220 	FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: BEGIN"));
       
  1221 	FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: aUid: '%d'"), aUid);
       
  1222 	
       
  1223 	TInt err(KErrNone);
       
  1224 	iCallerStatus = &aStatus;
       
  1225     *iCallerStatus = KRequestPending;
       
  1226     
       
  1227     // Leave is Data Store is in wrong state
       
  1228     if ( iState != ENSmlOpenAndWaiting )
       
  1229         {
       
  1230         User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1231         return;
       
  1232         }
       
  1233     
       
  1234     //Check validity of UID
       
  1235     if ( aUid <= 0 )
       
  1236         {
       
  1237         User::RequestComplete( iCallerStatus, KErrCorrupt );
       
  1238         return;
       
  1239         }
       
  1240 
       
  1241     // Check the Sync Status
       
  1242     if( iIsHierarchicalSyncSupported )
       
  1243         {
       
  1244         TCalLocalUid parentid( NULL );
       
  1245         TCalLocalUid entryid( aUid );
       
  1246        
       
  1247         TRAP( err, GetCalendarEntryIdL( parentid, entryid ));
       
  1248         if ( err )
       
  1249             {
       
  1250             FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: Invalid UID"));
       
  1251             User::RequestComplete( iCallerStatus, KErrGeneral );
       
  1252             FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL - Sync Disabled: END"));
       
  1253             return;
       
  1254             }    
       
  1255         
       
  1256         if( !iAgendaAdapterHandler->FolderSyncStatusL( parentid ) )
       
  1257             {
       
  1258             User::RequestComplete( iCallerStatus, KErrGeneral );
       
  1259             FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL - Sync Disabled: END"));
       
  1260             return;
       
  1261             }
       
  1262         }
       
  1263      else
       
  1264         {
       
  1265         TBool syncstatus( EFalse );
       
  1266         HBufC* calfilename = iOpenedStoreName->AllocL();
       
  1267         TRAP( err, syncstatus = iAgendaAdapterHandler->FolderSyncStatusL( calfilename ) );
       
  1268         if( err || !syncstatus )
       
  1269             {
       
  1270             delete calfilename;
       
  1271             User::RequestComplete( iCallerStatus, KErrGeneral );
       
  1272             FLOG(_L("CNSmlAgendaDataStore::DoReplaceItemL - Sync Disabled: END"));
       
  1273             return;
       
  1274             }
       
  1275         delete calfilename;            
       
  1276         }
       
  1277      
       
  1278     //Determine the Mime Type
       
  1279     DataMimeType( aUid );
       
  1280     switch( iDataMimeType )
       
  1281         {
       
  1282         case ENSmlFolder:
       
  1283             {        
       
  1284             HBufC* calfilename = NULL;
       
  1285             //Get the Calendar information
       
  1286             TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL(aUid) );
       
  1287             if( err != KErrNone || NULL == calfilename )
       
  1288                 {
       
  1289                 FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL:Invalid CalendarInfo"));
       
  1290                 User::RequestComplete( iCallerStatus, KErrNotFound );
       
  1291                 return;
       
  1292                 }
       
  1293             
       
  1294             FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: name exists"));
       
  1295             CCalSession* vCalSession = CCalSession::NewL();
       
  1296             CleanupStack::PushL(vCalSession);
       
  1297             vCalSession->OpenL(calfilename->Des());
       
  1298             FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: before deleting"));
       
  1299             TRAP(err, vCalSession->DeleteCalFileL(calfilename->Des()));
       
  1300             FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: after deleting err: '%d'"), err);
       
  1301             if( err == KErrInUse )
       
  1302                 {
       
  1303                 // Delete all the entries associated with this CalFile
       
  1304                 CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  1305                 TBuf8<KBuffLength> keyBuff;
       
  1306                 TInt aNumSuccessfulDeleted( 0 );
       
  1307                 RArray<TCalLocalUid> uidArray;
       
  1308                 CleanupClosePushL( uidArray );
       
  1309                 TCalTime zeroTime;
       
  1310                 zeroTime.SetTimeUtcL( Time::NullTTime() );
       
  1311                 
       
  1312                 agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  1313                 if( agendautil )
       
  1314                     {
       
  1315                     CleanupStack::PushL(agendautil);
       
  1316                     TRAP(err, agendautil->InitializeCalAPIsL( calfilename ));
       
  1317                     CleanupStack::Pop(agendautil);
       
  1318                     }                      
       
  1319                 if ( err || !agendautil )
       
  1320                     {
       
  1321                     FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL:Invalid CalendarInfo"));
       
  1322                     delete calfilename;
       
  1323                     User::RequestComplete( iCallerStatus, KErrNotFound );
       
  1324                     return;
       
  1325                     }            
       
  1326                 agendautil->iEntryView->GetIdsModifiedSinceDateL( zeroTime, uidArray );
       
  1327                 // ... and then delete them
       
  1328                 agendautil->iEntryView->DeleteL( uidArray, aNumSuccessfulDeleted );
       
  1329                 CleanupStack::PopAndDestroy( &uidArray ); // uidArray
       
  1330                 delete agendautil;    
       
  1331                 
       
  1332                 // Get the CalFile
       
  1333                 CCalCalendarInfo* caleninfo = vCalSession->CalendarInfoL();
       
  1334                 CleanupStack::PushL(caleninfo);
       
  1335                 
       
  1336                 // Mark the CalFile as Hidden
       
  1337                 caleninfo->SetEnabled( EFalse );
       
  1338                             
       
  1339                 // Set the SyncStatus to False
       
  1340                 keyBuff.Zero();
       
  1341                 keyBuff.AppendNum( ESyncStatus );
       
  1342                 TBool syncstatus( EFalse );
       
  1343                 TPckgC<TBool> pckgSyncStatusValue( syncstatus );
       
  1344                 caleninfo->SetPropertyL( keyBuff, pckgSyncStatusValue );
       
  1345                 
       
  1346                 // Mark the meta property as SoftDeleted
       
  1347                 keyBuff.Zero();
       
  1348                 keyBuff.AppendNum(EMarkAsDelete);
       
  1349                 TPckgC<TBool> pkgSoftDelete( ETrue );
       
  1350                 caleninfo->SetPropertyL(keyBuff, pkgSoftDelete);
       
  1351                 
       
  1352                 vCalSession->SetCalendarInfoL( *caleninfo );
       
  1353                 CleanupStack::PopAndDestroy(caleninfo);            
       
  1354                 }
       
  1355             else if( err != KErrNone )
       
  1356                 {
       
  1357                 CleanupStack::PopAndDestroy(vCalSession);
       
  1358                 delete calfilename;
       
  1359                 User::RequestComplete( iCallerStatus, KErrGeneral );
       
  1360                 return;
       
  1361                 }
       
  1362             CleanupStack::PopAndDestroy(vCalSession);            
       
  1363             delete calfilename;  
       
  1364             
       
  1365             //Update the array
       
  1366             TInt index = KErrNotFound;
       
  1367             TInt err = KErrNone;
       
  1368             TKeyArrayFix key( 0, ECmpTInt ); // Find key for Ids.
       
  1369        
       
  1370             err = iCalOffsetArr->Find( aUid, key, index );
       
  1371                   
       
  1372             if( err == KErrNone )
       
  1373                 {
       
  1374                 iCalOffsetArr->Delete(index);
       
  1375                 FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: updated the array"));
       
  1376                 }
       
  1377             }
       
  1378             break;
       
  1379         case ENSmlCalendar:
       
  1380             {
       
  1381             CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  1382             HBufC* calfilename = NULL;
       
  1383             TCalLocalUid parentid(NULL);
       
  1384             TCalLocalUid entryid(aUid);
       
  1385             TInt err( KErrNone );
       
  1386             
       
  1387             if( iIsHierarchicalSyncSupported )
       
  1388                 {
       
  1389                 TRAP( err, GetCalendarEntryIdL( parentid, entryid ));
       
  1390                 if ( err )
       
  1391                     {
       
  1392                     FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL:Parent Id is not Valid one"));
       
  1393                     User::RequestComplete( iCallerStatus, KErrNotFound );
       
  1394                     return;
       
  1395                     }
       
  1396                 //Get the Folder Information
       
  1397                 TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL(parentid) );
       
  1398                 }
       
  1399             else
       
  1400                 {
       
  1401                 calfilename = iOpenedStoreName->AllocL();
       
  1402                 }
       
  1403                 
       
  1404             if( err != KErrNone || NULL == calfilename )
       
  1405                 {
       
  1406                 FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL:Invalid CalendarInfo"));
       
  1407                 User::RequestComplete( iCallerStatus, KErrNotFound );
       
  1408                 return;
       
  1409                 }
       
  1410             
       
  1411             agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  1412             if( agendautil )
       
  1413                 {
       
  1414                 CleanupStack::PushL(agendautil);
       
  1415                 TRAP(err, agendautil->InitializeCalAPIsL( calfilename, entryid ));
       
  1416                 CleanupStack::Pop(agendautil);
       
  1417                 }                      
       
  1418             if ( err || !agendautil )
       
  1419                 {
       
  1420                 FLOG(_L("CNSmlAgendaDataStore::DoOpenItemL: entry is not valid"));
       
  1421                 delete agendautil;
       
  1422                 delete calfilename;
       
  1423                 User::RequestComplete( iCallerStatus, KErrNotFound );
       
  1424                 return;
       
  1425                 }                
       
  1426             agendautil->iEntryView->DeleteL( *agendautil->iEntry );
       
  1427             delete agendautil;
       
  1428             delete calfilename;
       
  1429             }
       
  1430             break;
       
  1431         default:
       
  1432             break;
       
  1433         }
       
  1434     
       
  1435     //Update the Snapshots
       
  1436     if ( iChangeFinder )
       
  1437         {
       
  1438         TNSmlSnapshotItem item( aUid );
       
  1439         iChangeFinder->ItemDeleted( item );        
       
  1440         }
       
  1441         
       
  1442     User::RequestComplete( iCallerStatus, KErrNone );
       
  1443 	    
       
  1444 	FLOG(_L("CNSmlAgendaDataStore::DoDeleteItemL: END"));
       
  1445     }
       
  1446 
       
  1447 // -----------------------------------------------------------------------------
       
  1448 // CNSmlAgendaDataStore::DoSoftDeleteItemL
       
  1449 // Delete item from database.
       
  1450 // -----------------------------------------------------------------------------
       
  1451 //
       
  1452 void CNSmlAgendaDataStore::DoSoftDeleteItemL( TSmlDbItemUid /*aUid*/,
       
  1453                                               TRequestStatus& aStatus )
       
  1454     {
       
  1455 	FLOG(_L("CNSmlAgendaDataStore::DoSoftDeleteItemL: BEGIN"));
       
  1456 	iCallerStatus = &aStatus;
       
  1457 	*iCallerStatus = KRequestPending;
       
  1458 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
  1459 	FLOG(_L("CNSmlAgendaDataStore::DoSoftDeleteItemL: END"));
       
  1460     }
       
  1461 
       
  1462 // -----------------------------------------------------------------------------
       
  1463 // CNSmlAgendaDataStore::DoDeleteAllItemsL
       
  1464 // Delete all items from database.
       
  1465 // -----------------------------------------------------------------------------
       
  1466 //
       
  1467 void CNSmlAgendaDataStore::DoDeleteAllItemsL( TRequestStatus& aStatus )
       
  1468     {
       
  1469     FLOG(_L("CNSmlAgendaDataStore::DoDeleteAllItemsL: BEGIN"));
       
  1470     iCallerStatus = &aStatus;
       
  1471     *iCallerStatus = KRequestPending;
       
  1472     if ( iState != ENSmlOpenAndWaiting ) 
       
  1473         {
       
  1474         User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1475         return;
       
  1476         }
       
  1477     
       
  1478     if( iIsHierarchicalSyncSupported )
       
  1479         {
       
  1480         // TODO: Have to enable once the delete issue is fixed by Organizer
       
  1481         FLOG(_L("CNSmlAgendaDataStore::DoDeleteAllItemsL: Temporarily doesnot support"));
       
  1482         User::RequestComplete( iCallerStatus, KErrNotSupported );
       
  1483         return;
       
  1484         }
       
  1485     else
       
  1486         {
       
  1487         CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  1488         HBufC* calfilename = NULL;
       
  1489         
       
  1490         agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  1491         calfilename = iOpenedStoreName->AllocL();
       
  1492         if( agendautil )
       
  1493             {
       
  1494             CleanupStack::PushL(agendautil);
       
  1495             agendautil->InitializeCalAPIsL( calfilename ); 
       
  1496             CleanupStack::Pop(agendautil);
       
  1497             // Delete all items
       
  1498             // First searh every UIDs ...
       
  1499             TInt aNumSuccessfulDeleted( 0 );
       
  1500             RArray<TCalLocalUid> uidArray;
       
  1501             CleanupClosePushL( uidArray );
       
  1502             TCalTime zeroTime;
       
  1503             zeroTime.SetTimeUtcL( Time::NullTTime() );
       
  1504             agendautil->iEntryView->GetIdsModifiedSinceDateL( zeroTime, uidArray );
       
  1505             
       
  1506             // ... and then delete them
       
  1507             agendautil->iEntryView->DeleteL( uidArray, aNumSuccessfulDeleted );
       
  1508             CleanupStack::PopAndDestroy( &uidArray ); // uidArray            
       
  1509             }
       
  1510         delete agendautil;
       
  1511         delete calfilename;
       
  1512         }
       
  1513         
       
  1514     // Update changefinder
       
  1515     if ( iChangeFinder )
       
  1516         {
       
  1517         iChangeFinder->ResetL();
       
  1518         }
       
  1519     iSnapshotRegistered = EFalse;
       
  1520     RegisterSnapshotL();
       
  1521     
       
  1522     User::RequestComplete( iCallerStatus, KErrNone );
       
  1523     
       
  1524     FLOG(_L("CNSmlAgendaDataStore::DoDeleteAllItemsL: END"));
       
  1525     }
       
  1526 
       
  1527 // -----------------------------------------------------------------------------
       
  1528 // CNSmlAgendaDataStore::DoHasSyncHistory
       
  1529 // Return ETrue if syncronization history is available.
       
  1530 // -----------------------------------------------------------------------------
       
  1531 //
       
  1532 TBool CNSmlAgendaDataStore::DoHasSyncHistory() const
       
  1533     {
       
  1534 	FLOG(_L("CNSmlAgendaDataStore::DoHasSyncHistory: BEGIN"));
       
  1535 	TBool ret = EFalse;
       
  1536 	if ( iHasHistory )
       
  1537 		{
       
  1538 		if ( iOpenedStoreId != iChangeFinder->DataStoreUid() )
       
  1539 			{
       
  1540 			iChangeFinder->SetDataStoreUid( iOpenedStoreId );
       
  1541 			}
       
  1542 		else
       
  1543 			{
       
  1544 			ret = ETrue;
       
  1545 			}
       
  1546 		}
       
  1547 	else
       
  1548 		{
       
  1549 		iChangeFinder->SetDataStoreUid( iOpenedStoreId );
       
  1550 		}
       
  1551 	FLOG(_L("CNSmlAgendaDataStore::DoHasSyncHistory: END"));
       
  1552 	return ret;
       
  1553     }
       
  1554 
       
  1555 // -----------------------------------------------------------------------------
       
  1556 // CNSmlAgendaDataStore::DoAddedItems
       
  1557 // Give uid list of added items since last syncronization.
       
  1558 // -----------------------------------------------------------------------------
       
  1559 //
       
  1560 const MSmlDataItemUidSet& CNSmlAgendaDataStore::DoAddedItems() const
       
  1561     {
       
  1562 	FLOG(_L("CNSmlAgendaDataStore::DoAddedItems: BEGIN"));
       
  1563 	if ( iState == ENSmlOpenAndWaiting )
       
  1564 		{
       
  1565 		iNewUids->Reset();
       
  1566 		TRAP_IGNORE( iChangeFinder->FindNewItemsL( *iNewUids ) );
       
  1567 		
       
  1568 		// RD_MULTICAL		
       
  1569         if( iIsHierarchicalSyncSupported )
       
  1570             {
       
  1571             CNSmlDataItemUidSet* inactiveuids = NULL;
       
  1572             TRAP_IGNORE(inactiveuids = ActiveItemsL( *iNewUids ));
       
  1573             delete inactiveuids;
       
  1574             }
       
  1575         else
       
  1576             {
       
  1577             TBool syncstatus( EFalse );
       
  1578             HBufC* calfilename = NULL;
       
  1579             TRAP_IGNORE( calfilename = iOpenedStoreName->AllocL() );
       
  1580             TRAP_IGNORE( syncstatus = iAgendaAdapterHandler->FolderSyncStatusL( calfilename ) );
       
  1581             if( !syncstatus )
       
  1582                 {
       
  1583                 iNewUids->Reset();
       
  1584                 }
       
  1585             delete calfilename;
       
  1586             }
       
  1587         // RD_MULTICAL		
       
  1588 		}
       
  1589 	
       
  1590 	for( TInt count = 0; count < iNewUids->ItemCount(); count++ )
       
  1591         {
       
  1592         FLOG(_L("CNSmlAgendaDataStore::DoAddedItems: list '%d'"), iNewUids->ItemAt(count) );
       
  1593         }
       
  1594 	
       
  1595 	FLOG(_L("CNSmlAgendaDataStore::DoAddedItems: END"));
       
  1596 	return *iNewUids;
       
  1597     }
       
  1598 
       
  1599 // -----------------------------------------------------------------------------
       
  1600 // CNSmlAgendaDataStore::DoDeletedItems
       
  1601 // Give uid list of deleted items since last syncronization.
       
  1602 // -----------------------------------------------------------------------------
       
  1603 //
       
  1604 const MSmlDataItemUidSet& CNSmlAgendaDataStore::DoDeletedItems() const
       
  1605     {
       
  1606 	FLOG(_L("CNSmlAgendaDataStore::DoDeletedItems: BEGIN"));
       
  1607 	if ( iState == ENSmlOpenAndWaiting )
       
  1608 		{
       
  1609 		iDeletedUids->Reset();
       
  1610 		TRAP_IGNORE( iChangeFinder->FindDeletedItemsL( *iDeletedUids ) );		
       
  1611 	
       
  1612     	// RD_MULTICAL
       
  1613     	if( iIsHierarchicalSyncSupported )
       
  1614     	    {
       
  1615         	CArrayFixFlat<TInt>* folderuidarr; 
       
  1616         	CNSmlDataItemUidSet* tempdeleteuids; 
       
  1617         	TSmlDbItemUid uid(0);
       
  1618         	
       
  1619         	folderuidarr = new CArrayFixFlat<TInt>( KArrayGranularity );
       
  1620         	tempdeleteuids= new CNSmlDataItemUidSet();
       
  1621         	
       
  1622         	// Get the list of Folder ids
       
  1623         	for( TInt count =0; count < iDeletedUids->ItemCount(); count++ )
       
  1624                 {
       
  1625                 uid = iDeletedUids->ItemAt(count);
       
  1626                 
       
  1627                 if( 0 == (uid % iCalOffsetVal) )
       
  1628                     {
       
  1629                     TRAP_IGNORE( folderuidarr->AppendL(uid) );
       
  1630                     FLOG(_L("CNSmlAgendaDataStore::DoDeletedItems: Folderuid: '%d'"), uid);
       
  1631                     }        
       
  1632                 }
       
  1633         	
       
  1634         	CNSmlDataItemUidSet* inactiveuids = NULL;
       
  1635         	TRAP_IGNORE( inactiveuids = ActiveItemsL( *iDeletedUids ) );
       
  1636             delete inactiveuids;
       
  1637         	
       
  1638         	if( folderuidarr->Count() > 0 )
       
  1639         	    {
       
  1640         	    TInt index = KErrNotFound;
       
  1641                 TInt err = KErrNone;
       
  1642                 TKeyArrayFix key( 0, ECmpTInt ); // Find key for Ids.
       
  1643                 
       
  1644                 // Fill the temp uid array
       
  1645                 for (TInt folderidcount = 0; folderidcount< folderuidarr->Count(); folderidcount++)
       
  1646                     {
       
  1647                     tempdeleteuids->AddItem(folderuidarr->At(folderidcount));
       
  1648                     }
       
  1649                 
       
  1650         	    // Filter out ID's of entries for Folder deletion
       
  1651                 for( TInt count =0; count < iDeletedUids->ItemCount(); count++ )
       
  1652                     {
       
  1653                     uid = iDeletedUids->ItemAt(count);
       
  1654                     uid = (uid/iCalOffsetVal)*iCalOffsetVal;
       
  1655                     err = folderuidarr->Find( uid, key, index );
       
  1656                     if (err == KErrNone)
       
  1657                         {
       
  1658                         break;
       
  1659                         }
       
  1660                     tempdeleteuids->AddItem(uid);
       
  1661                     }
       
  1662                 // Store the proper UID values
       
  1663                 iDeletedUids->Reset();
       
  1664                 for( TInt count =0; count < tempdeleteuids->ItemCount(); count++ )
       
  1665                     {
       
  1666                     iDeletedUids->AddItem(tempdeleteuids->ItemAt(count));
       
  1667                     }
       
  1668         	    }	
       
  1669         	delete tempdeleteuids;
       
  1670         	delete folderuidarr;
       
  1671     	    }
       
  1672     	else
       
  1673     	    {
       
  1674     	    TBool syncstatus( EFalse );
       
  1675     	    HBufC* calfilename = NULL;
       
  1676     	    TRAP_IGNORE( calfilename = iOpenedStoreName->AllocL() );
       
  1677     	    TRAP_IGNORE( syncstatus = iAgendaAdapterHandler->FolderSyncStatusL( calfilename ) );
       
  1678             if( !syncstatus )
       
  1679                 {
       
  1680                 iDeletedUids->Reset();
       
  1681                 }
       
  1682             delete calfilename;
       
  1683     	    }	    
       
  1684     	// RD_MULTICAL
       
  1685 		}
       
  1686 	
       
  1687 	for( TInt count = 0; count < iDeletedUids->ItemCount(); count++ )
       
  1688         {
       
  1689         FLOG(_L("CNSmlAgendaDataStore::DoDeletedItems: list '%d'"), iDeletedUids->ItemAt(count) );
       
  1690         }
       
  1691 	
       
  1692 	FLOG(_L("CNSmlAgendaDataStore::DoDeletedItems: END"));	
       
  1693 	return *iDeletedUids;
       
  1694     }
       
  1695 
       
  1696 // -----------------------------------------------------------------------------
       
  1697 // CNSmlAgendaDataStore::DoSoftDeletedItems
       
  1698 // Give uid list of deleted items since last syncronization.
       
  1699 // -----------------------------------------------------------------------------
       
  1700 //
       
  1701 const MSmlDataItemUidSet& CNSmlAgendaDataStore::DoSoftDeletedItems() const
       
  1702     {
       
  1703 	FLOG(_L("CNSmlAgendaDataStore::DoSoftDeletedItems: BEGIN"));
       
  1704 	if ( iState == ENSmlOpenAndWaiting )
       
  1705 		{
       
  1706 		iSoftDeletedUids->Reset();
       
  1707 		TRAP_IGNORE(
       
  1708 		    iChangeFinder->FindSoftDeletedItemsL( *iSoftDeletedUids ) );
       
  1709 		}
       
  1710 	FLOG(_L("CNSmlAgendaDataStore::DoSoftDeletedItems: END"));
       
  1711 	return *iSoftDeletedUids;
       
  1712     }
       
  1713 
       
  1714 // -----------------------------------------------------------------------------
       
  1715 // CNSmlAgendaDataStore::DoModifiedItems
       
  1716 // Give uid list of modified items since last syncronization.
       
  1717 // -----------------------------------------------------------------------------
       
  1718 //
       
  1719 const MSmlDataItemUidSet& CNSmlAgendaDataStore::DoModifiedItems() const
       
  1720     {
       
  1721 	FLOG(_L("CNSmlAgendaDataStore::DoModifiedItems: BEGIN"));
       
  1722 	if ( iState == ENSmlOpenAndWaiting )
       
  1723 		{
       
  1724 		iReplacedUids->Reset();
       
  1725 		TRAP_IGNORE( iChangeFinder->FindChangedItemsL( *iReplacedUids ) );
       
  1726 		
       
  1727 		// RD_MULTICAL
       
  1728 	    if( iIsHierarchicalSyncSupported )
       
  1729             {
       
  1730             CNSmlDataItemUidSet* inactiveuids = NULL;
       
  1731             TRAP_IGNORE( inactiveuids = ActiveItemsL( *iReplacedUids ) );
       
  1732             
       
  1733             if( inactiveuids )
       
  1734                 {
       
  1735                 TRAP_IGNORE( InternalizeCommittedUidL() );
       
  1736                 for( TInt count = 0; count < inactiveuids->ItemCount(); count++ )
       
  1737                     {
       
  1738                     TSmlDbItemUid inactiveuid = inactiveuids->ItemAt(count);
       
  1739                     FLOG(_L("CNSmlAgendaDataStore::DoModifiedItems: inactive '%d'"), inactiveuid );
       
  1740                     for( TInt uidcount = 0; uidcount < iCommittedUidArr->ItemCount(); uidcount++ )
       
  1741                         {
       
  1742                         if( inactiveuid == iCommittedUidArr->ItemAt( uidcount ) )
       
  1743                             {
       
  1744                             FLOG(_L("CNSmlAgendaDataStore::DoModifiedItems: '%d'"), inactiveuid );
       
  1745                             iReplacedUids->AddItem( inactiveuid );
       
  1746                             }
       
  1747                         }
       
  1748                     }            
       
  1749                 delete inactiveuids;
       
  1750                 }
       
  1751             }
       
  1752         else
       
  1753             {
       
  1754             TBool syncstatus( EFalse );
       
  1755             HBufC* calfilename = NULL;
       
  1756             TRAP_IGNORE( calfilename= iOpenedStoreName->AllocL() );
       
  1757             TRAP_IGNORE( syncstatus = iAgendaAdapterHandler->FolderSyncStatusL( calfilename ) );
       
  1758             if( !syncstatus )
       
  1759                 {
       
  1760                 iReplacedUids->Reset();
       
  1761                 }
       
  1762             delete calfilename;
       
  1763             }	    
       
  1764         // RD_MULTICAL
       
  1765 		}
       
  1766 	
       
  1767 	 for( TInt count = 0; count < iReplacedUids->ItemCount(); count++ )
       
  1768 	     {
       
  1769 	     FLOG(_L("CNSmlAgendaDataStore::DoModifiedItems: list '%d'"), iReplacedUids->ItemAt(count) );
       
  1770 	     }
       
  1771 	
       
  1772 	FLOG(_L("CNSmlAgendaDataStore::DoModifiedItems: END"));
       
  1773 	return *iReplacedUids;
       
  1774     }
       
  1775 
       
  1776 // -----------------------------------------------------------------------------
       
  1777 // CNSmlAgendaDataStore::DoMovedItems
       
  1778 // Give uid list of moved items since last syncronization.
       
  1779 // -----------------------------------------------------------------------------
       
  1780 //
       
  1781 const MSmlDataItemUidSet& CNSmlAgendaDataStore::DoMovedItems() const
       
  1782     {
       
  1783 	FLOG(_L("CNSmlAgendaDataStore::DoMovedItems: BEGIN"));
       
  1784 	if ( iState == ENSmlOpenAndWaiting )
       
  1785 		{
       
  1786 		iMovedUids->Reset();
       
  1787 		TRAP_IGNORE( iChangeFinder->FindMovedItemsL( *iMovedUids ) );
       
  1788 		}
       
  1789 	
       
  1790 	for( TInt count = 0; count < iMovedUids->ItemCount(); count++ )
       
  1791         {
       
  1792         FLOG(_L("CNSmlAgendaDataStore::DoMovedItems: list '%d'"), iMovedUids->ItemAt(count) );
       
  1793         }
       
  1794 	
       
  1795 	FLOG(_L("CNSmlAgendaDataStore::DoMovedItems: END"));
       
  1796 	return *iMovedUids;
       
  1797     }
       
  1798 
       
  1799 // -----------------------------------------------------------------------------
       
  1800 // CNSmlAgendaDataStore::DoResetChangeInfoL
       
  1801 // Reset change info that exist since last syncronization.
       
  1802 // -----------------------------------------------------------------------------
       
  1803 //
       
  1804 void CNSmlAgendaDataStore::DoResetChangeInfoL( TRequestStatus& aStatus )
       
  1805     {
       
  1806 	FLOG(_L("CNSmlAgendaDataStore::DoResetChangeInfoL: BEGIN"));
       
  1807 	iCallerStatus = &aStatus;
       
  1808 	*iCallerStatus = KRequestPending;
       
  1809 	if ( iState != ENSmlOpenAndWaiting ) 
       
  1810 		{
       
  1811 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1812 		return;
       
  1813 		}
       
  1814 	iChangeFinder->ResetL();
       
  1815 	iSnapshotRegistered = EFalse;
       
  1816 	if( !iSnapshotRegistered )
       
  1817 		{
       
  1818 		RegisterSnapshotL();
       
  1819 		}
       
  1820 	User::RequestComplete( iCallerStatus, KErrNone );
       
  1821 	FLOG(_L("CNSmlAgendaDataStore::DoResetChangeInfoL: END"));
       
  1822     }
       
  1823 
       
  1824 // -----------------------------------------------------------------------------
       
  1825 // CNSmlAgendaDataStore::DoCommitChangeInfoL
       
  1826 // Commit change info that exist since last syncronization for given uid list.
       
  1827 // -----------------------------------------------------------------------------
       
  1828 //
       
  1829 void CNSmlAgendaDataStore::DoCommitChangeInfoL( TRequestStatus& aStatus,
       
  1830                 const MSmlDataItemUidSet& aItems )
       
  1831     {
       
  1832 	FLOG(_L("CNSmlAgendaDataStore::DoCommitChangeInfoL: BEGIN"));
       
  1833 	iCallerStatus = &aStatus;
       
  1834 	*iCallerStatus = KRequestPending;
       
  1835 	if ( iState != ENSmlOpenAndWaiting ) 
       
  1836 		{
       
  1837 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1838 		return;
       
  1839 		}
       
  1840 	iChangeFinder->CommitChangesL( aItems );
       
  1841 	
       
  1842 	// Save the UIDs to the list
       
  1843 	iCommittedUidArr->Reset();
       
  1844 	for ( TInt count = 0; count < aItems.ItemCount(); count++ )
       
  1845         {
       
  1846         iCommittedUidArr->AddItem( aItems.ItemAt( count ) );
       
  1847         }
       
  1848 	CNSmlDataItemUidSet* inactiveuids = NULL;
       
  1849 	TRAP_IGNORE( inactiveuids = ActiveItemsL( *iCommittedUidArr ) ); 
       
  1850 	delete inactiveuids;
       
  1851 	
       
  1852 	// Print the iCommittedUidArr array
       
  1853 	FLOG(_L("CNSmlAgendaAdapter::DoCommitChangeInfoL(): CommittedUidArr"));
       
  1854     for( TInt count = 0; count < iCommittedUidArr->ItemCount(); count++ )
       
  1855         {
       
  1856         FLOG(_L("CNSmlAgendaDataStore::iCommittedUidArr: list '%d'"), iCommittedUidArr->ItemAt(count) );
       
  1857         }   
       
  1858     
       
  1859     // Store the array in the stream
       
  1860     ExternalizeCommittedUidL();
       
  1861 	User::RequestComplete( iCallerStatus, KErrNone );
       
  1862 	FLOG(_L("CNSmlAgendaDataStore::DoCommitChangeInfoL: END"));
       
  1863     }
       
  1864 
       
  1865 // -----------------------------------------------------------------------------
       
  1866 // CNSmlAgendaDataStore::DoCommitChangeInfoL
       
  1867 // Commit change info that exist since last syncronization.
       
  1868 // -----------------------------------------------------------------------------
       
  1869 //
       
  1870 void CNSmlAgendaDataStore::DoCommitChangeInfoL( TRequestStatus& aStatus )
       
  1871     {
       
  1872 	FLOG(_L("CNSmlAgendaDataStore::DoCommitChangeInfoL: BEGIN"));
       
  1873 	iCallerStatus = &aStatus;
       
  1874 	*iCallerStatus = KRequestPending;
       
  1875 	if ( iState != ENSmlOpenAndWaiting ) 
       
  1876 		{
       
  1877 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1878 		return;
       
  1879 		}
       
  1880 	iChangeFinder->CommitChangesL();
       
  1881     // Print the iInterCommittedUidArr array
       
  1882     FLOG(_L("CNSmlAgendaAdapter::DoCommitChangeInfoL() Without Param: CommittedUidArr"));
       
  1883     for( TInt count = 0; count < iCommittedUidArr->ItemCount(); count++ )
       
  1884         {
       
  1885         FLOG(_L("CNSmlAgendaDataStore::iCommittedUidArr: list '%d'"), iCommittedUidArr->ItemAt(count) );
       
  1886         }
       
  1887     
       
  1888     // Store the array in the stream
       
  1889     ExternalizeCommittedUidL();
       
  1890 	User::RequestComplete( iCallerStatus, KErrNone );
       
  1891 	FLOG(_L("CNSmlAgendaDataStore::DoCommitChangeInfoL: END"));
       
  1892     }
       
  1893     
       
  1894 // -----------------------------------------------------------------------------
       
  1895 // CNSmlAgendaDataStore::RegisterSnapshotL
       
  1896 // Register snapshot.
       
  1897 // -----------------------------------------------------------------------------
       
  1898 //
       
  1899 void CNSmlAgendaDataStore::RegisterSnapshotL()
       
  1900     {
       
  1901 	FLOG(_L("CNSmlAgendaAdapter::RegisterSnapshotL(): begin"));
       
  1902 	CArrayFixSeg<TNSmlSnapshotItem>* snapshot =
       
  1903 	                new ( ELeave ) CArrayFixSeg<TNSmlSnapshotItem>( 64 );
       
  1904 	CleanupStack::PushL( snapshot );
       
  1905 	RArray<TCalLocalUid> uidArray;
       
  1906 	CleanupClosePushL( uidArray );
       
  1907 	
       
  1908 	// RD_MULTICAL
       
  1909 	if( iIsHierarchicalSyncSupported )
       
  1910 	    {
       
  1911     	// First find all entries ...
       
  1912 	    iAgendaAdapterHandler->SynchronizableCalendarIdsL( iCalOffsetArr );  
       
  1913 	    // Populate the Entry ID's associated with the CalFile
       
  1914 	    SynchronizableCalEntryIdsL( uidArray );
       
  1915         }
       
  1916 	else
       
  1917 	    {
       
  1918 	    HBufC* calfilename = iOpenedStoreName->AllocL();
       
  1919 	    CNSmlAgendaDataStoreUtil* agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  1920         if( agendautil )
       
  1921             {
       
  1922             CleanupStack::PushL(agendautil);
       
  1923             agendautil->InitializeCalAPIsL( calfilename );
       
  1924             CleanupStack::Pop(agendautil);
       
  1925             }	   
       
  1926 	    // First find all entries ...
       
  1927         TCalTime zeroTime;
       
  1928         zeroTime.SetTimeUtcL( Time::NullTTime() );
       
  1929         agendautil->iEntryView->GetIdsModifiedSinceDateL( zeroTime, uidArray );
       
  1930         delete agendautil;
       
  1931         delete calfilename;
       
  1932 	    }
       
  1933 	// RD_MULTICAL
       
  1934     
       
  1935 	// ... and then create snapshot items
       
  1936 	for ( TInt i = 0; i < uidArray.Count(); i++ )
       
  1937 	    {
       
  1938 	    TNSmlSnapshotItem newItem = CreateSnapshotItemL( uidArray[i] );
       
  1939 	    if ( newItem.ItemId() != 0 )
       
  1940 	        {
       
  1941 	        snapshot->InsertIsqL( newItem, iKey );
       
  1942 	        }
       
  1943 	    }
       
  1944 	
       
  1945 	CleanupStack::PopAndDestroy( &uidArray );	
       
  1946 	
       
  1947     for ( TInt i = 0; i < snapshot->Count(); i++ )
       
  1948         {
       
  1949         TNSmlSnapshotItem item = snapshot->At(i);
       
  1950         FLOG(_L("CNSmlAgendaAdapter::RegisterSnapshotL(): id: '%d'"), item.ItemId());
       
  1951         }
       
  1952 		
       
  1953 	iChangeFinder->SetNewSnapshot( snapshot );
       
  1954 	
       
  1955 	// Save the UIDs to the list
       
  1956 	iCommittedUidArr->Reset();
       
  1957 	for ( TInt count = 0; count < snapshot->Count(); count++ )
       
  1958         {
       
  1959         iCommittedUidArr->AddItem( snapshot->At( count ).ItemId() );
       
  1960         }
       
  1961 	CNSmlDataItemUidSet* inactiveuids = NULL;
       
  1962 	TRAP_IGNORE( inactiveuids = ActiveItemsL( *iCommittedUidArr ) );
       
  1963 	delete inactiveuids;
       
  1964 	// Print the iCommittedUidArr array
       
  1965 	FLOG(_L("CNSmlAgendaAdapter::RegisterSnapshotL(): CommittedUidArr"));
       
  1966 	for( TInt count = 0; count < iCommittedUidArr->ItemCount(); count++ )
       
  1967         {
       
  1968         FLOG(_L("CNSmlAgendaDataStore::RegisterSnapshotL: list '%d'"), iCommittedUidArr->ItemAt(count) );
       
  1969         }
       
  1970 	
       
  1971 	// iChangeFinder takes ownership of items
       
  1972 	CleanupStack::Pop( snapshot );
       
  1973 	    
       
  1974 	iSnapshotRegistered = ETrue;
       
  1975 	
       
  1976 	FLOG(_L("CNSmlAgendaAdapter::RegisterSnapshotL(): end"));
       
  1977     }
       
  1978 
       
  1979 // -----------------------------------------------------------------------------
       
  1980 // CNSmlAgendaDataStore::CreateSnapshotItemL
       
  1981 // Creates new snapshot. Method gets data from database.
       
  1982 // -----------------------------------------------------------------------------
       
  1983 TNSmlSnapshotItem CNSmlAgendaDataStore::CreateSnapshotItemL(
       
  1984                 const TCalLocalUid& aUid )
       
  1985     {
       
  1986     FLOG(_L("CNSmlAgendaAdapter::CreateSnapshotItemL(): Begin"));
       
  1987     
       
  1988     TNSmlSnapshotItem item( 0 );
       
  1989     DataMimeType( aUid );
       
  1990     
       
  1991     switch( iDataMimeType )
       
  1992         {
       
  1993         case ENSmlFolder:
       
  1994             {
       
  1995             item = iAgendaAdapterHandler->CreateFolderSnapShotItemL( aUid );
       
  1996             }
       
  1997             break;
       
  1998         case ENSmlCalendar:
       
  1999             {
       
  2000             CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  2001             HBufC* calfilename = NULL;
       
  2002             TCalLocalUid parentid(NULL);
       
  2003             TCalLocalUid entryid(aUid);
       
  2004             TInt err( KErrNone );
       
  2005             
       
  2006             if( iIsHierarchicalSyncSupported )
       
  2007                 {
       
  2008                 TRAP( err, GetCalendarEntryIdL( parentid, entryid ));
       
  2009                 if ( err )
       
  2010                     {
       
  2011                     FLOG(_L("CNSmlAgendaDataStore::CreateSnapshotItemL:Parent Id is not Valid one"));
       
  2012                     return item;
       
  2013                     }
       
  2014                 //Get the Folder Information
       
  2015                 TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL(parentid) );
       
  2016                 }  
       
  2017             else
       
  2018                 {
       
  2019                 calfilename = iOpenedStoreName->AllocL();
       
  2020                 }
       
  2021                 
       
  2022              if( err != KErrNone || NULL == calfilename )
       
  2023                 {
       
  2024                 FLOG(_L("CNSmlAgendaDataStore::CreateSnapshotItemL:Invalid CalendarInfo"));
       
  2025                 return item;
       
  2026                 }
       
  2027             
       
  2028             FLOG(_L("CNSmlAgendaDataStore::CreateSnapshotItemL:Parent Id: '%d'"), parentid);
       
  2029             FLOG(_L("CNSmlAgendaDataStore::CreateSnapshotItemL:Entry Id: '%d'"), entryid);
       
  2030             
       
  2031             agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  2032             if( agendautil )
       
  2033                 {
       
  2034                 CleanupStack::PushL(agendautil);
       
  2035                 TRAP(err, agendautil->InitializeCalAPIsL( calfilename, entryid ));
       
  2036                 CleanupStack::Pop(agendautil);
       
  2037                 }            
       
  2038             if ( err || !agendautil )
       
  2039                 {
       
  2040                 FLOG(_L("CNSmlAgendaDataStore::CreateSnapshotItemL: entry is not valid"));  
       
  2041                 delete agendautil;
       
  2042                 delete calfilename; 
       
  2043                 return item;
       
  2044                 }
       
  2045          
       
  2046             CCalEntry::TReplicationStatus replicationStatus =
       
  2047                                             agendautil->iEntry->ReplicationStatusL();
       
  2048             if ( CanBeSynchronized( replicationStatus ) )
       
  2049                 {
       
  2050                 TUint intUid = agendautil->iEntry->LocalUidL() + parentid;
       
  2051                 item.SetItemId( intUid );
       
  2052                 item.SetLastChangedDate(
       
  2053                         agendautil->iEntry->LastModifiedDateL().TimeUtcL() );
       
  2054                 item.SetSoftDelete( EFalse );
       
  2055                 }
       
  2056             delete agendautil;
       
  2057             delete calfilename;
       
  2058             }
       
  2059             break;
       
  2060         default:
       
  2061             break;
       
  2062         }
       
  2063     FLOG(_L("CNSmlAgendaAdapter::CreateSnapshotItemL(): end"));
       
  2064     
       
  2065 	return item;
       
  2066     }
       
  2067 
       
  2068 // -----------------------------------------------------------------------------
       
  2069 // CNSmlAgendaDataStore::DoListAgendaFilesL
       
  2070 // List possible calendar database file names.
       
  2071 // -----------------------------------------------------------------------------
       
  2072 //
       
  2073 CDesCArray* CNSmlAgendaDataStore::DoListAgendaFilesLC() const
       
  2074     {
       
  2075     CDesCArray* array = new (ELeave) CDesCArrayFlat(1);
       
  2076     array->AppendL(*iDefaultStoreFileName);
       
  2077     CleanupStack::PushL( array );
       
  2078     return array;
       
  2079     }
       
  2080 
       
  2081 // -----------------------------------------------------------------------------
       
  2082 // CNSmlAgendaDataStore::DoGetDefaultFileNameL
       
  2083 // Return default calendar database name.
       
  2084 // -----------------------------------------------------------------------------
       
  2085 //
       
  2086 const TDesC& CNSmlAgendaDataStore::DoGetDefaultFileNameL() const
       
  2087     {
       
  2088 	if ( !iDefaultStoreFileName )
       
  2089 		{
       
  2090         User::Leave( KErrGeneral );
       
  2091 		}
       
  2092 	return *iDefaultStoreFileName;
       
  2093     }
       
  2094 
       
  2095 // -----------------------------------------------------------------------------
       
  2096 // CNSmlAgendaDataStore::CanBeSynchronized
       
  2097 // Return ETrue if entry can be synchronized.
       
  2098 // -----------------------------------------------------------------------------
       
  2099 //
       
  2100 TBool CNSmlAgendaDataStore::CanBeSynchronized(
       
  2101             const CCalEntry::TReplicationStatus&
       
  2102             aReplicationStatus ) const
       
  2103 	{
       
  2104 	return ( aReplicationStatus != CCalEntry::ERestricted );	
       
  2105 	}
       
  2106 
       
  2107 // -----------------------------------------------------------------------------
       
  2108 // CNSmlAgendaDataStore::DoOwnStoreFormatL
       
  2109 // Returns adapters supported store format which is read from Calendar Plug
       
  2110 // In Adapter own resource file.
       
  2111 // -----------------------------------------------------------------------------
       
  2112 //
       
  2113 CSmlDataStoreFormat* CNSmlAgendaDataStore::DoOwnStoreFormatL()
       
  2114 	{
       
  2115 	FLOG(_L("CNSmlAgendaDataStore:::DoOwnStoreFormatL(): BEGIN"));
       
  2116 	
       
  2117 	AgendaAdapterHandlerL();
       
  2118 	
       
  2119 	if( NULL == iAgendaAdapterHandler )
       
  2120 	    {
       
  2121         FLOG(_L("CNSmlAgendaDataStore:::DoOwnStoreFormatL(): Invalid AgendaAdapterHandler Error END"));
       
  2122         User::Leave( KErrGeneral );
       
  2123 	    }
       
  2124 	
       
  2125     TFileName fileName;
       
  2126     TParse* parse = NULL;
       
  2127     parse = new(ELeave) TParse();
       
  2128     CleanupStack::PushL( parse );
       
  2129     
       
  2130     // Check correct Data Sync protocol
       
  2131     TInt value( EDataSyncNotRunning );
       
  2132     TInt error = RProperty::Get( KPSUidDataSynchronizationInternalKeys,
       
  2133                                  KDataSyncStatus,
       
  2134                                  value );
       
  2135     
       
  2136     FLOG(_L("CNSmlAgendaDataStore:::DoOwnStoreFormatL(): SyncStatus: '%d'"), KDataSyncStatus);
       
  2137     FLOG(_L("CNSmlAgendaDataStore:::DoOwnStoreFormatL(): value: '%d'"), value);
       
  2138     
       
  2139     if ( error == KErrNone &&
       
  2140          value == EDataSyncRunning )
       
  2141         {
       
  2142         parse->Set( KNSmlDSAgendaDataStoreRsc_1_1_2,
       
  2143                           &KDC_RESOURCE_FILES_DIR, NULL );
       
  2144         }
       
  2145     else
       
  2146         { 
       
  2147         FLOG(_L("CNSmlAgendaDataStore:::DoOwnStoreFormatL(): Invoking AdapterHandler Implementation"));
       
  2148         CleanupStack::PopAndDestroy( parse );
       
  2149         return iAgendaAdapterHandler->StoreFormatL( iStringPool );
       
  2150         }
       
  2151     
       
  2152     fileName = parse->FullName();
       
  2153     RResourceFile resourceFile;
       
  2154     BaflUtils::NearestLanguageFile( iRfs, fileName );
       
  2155 
       
  2156     TRAPD( leavecode, resourceFile.OpenL( iRfs,fileName ) );
       
  2157     if ( leavecode != 0 )
       
  2158         {
       
  2159         CleanupStack::PopAndDestroy(); // parse
       
  2160         FLOG(_L("CNSmlAgendaDataStore:::StoreFormatL(): Error END"));
       
  2161         User::Leave( leavecode );
       
  2162         }
       
  2163     
       
  2164     CleanupClosePushL( resourceFile );
       
  2165     HBufC8* buffer = resourceFile.AllocReadLC( NSML_AGENDA_DATA_STORE );
       
  2166     TResourceReader reader;
       
  2167     reader.SetBuffer( buffer );
       
  2168 
       
  2169     CSmlDataStoreFormat* dsFormat = NULL;
       
  2170     dsFormat = CSmlDataStoreFormat::NewLC( iStringPool, reader );
       
  2171     CleanupStack::Pop(); // dsFormat
       
  2172     CleanupStack::PopAndDestroy( 3 ); // buffer, resourceFile, parse
       
  2173     
       
  2174     FLOG(_L("CNSmlAgendaDataStore::DoOwnStoreFormatL: END"));
       
  2175     return dsFormat;
       
  2176 	}
       
  2177 
       
  2178 // -----------------------------------------------------------------------------
       
  2179 // CNSmlAgendaDataStore::DoCommitCreateCalItemL
       
  2180 // Commit Calendar item data to database when adding item.
       
  2181 // -----------------------------------------------------------------------------
       
  2182 //
       
  2183 void CNSmlAgendaDataStore::DoCommitCreateCalItemL()
       
  2184     {
       
  2185     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: BEGIN"));
       
  2186     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: Parentid: '%d'"), iParentItemId);
       
  2187     iState = ENSmlOpenAndWaiting; // iState set to closed to handle leave
       
  2188 	CCalEntry::TReplicationStatus  replicationStatus;
       
  2189 	TInt err(KErrNone);
       
  2190 	CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  2191 	HBufC* calfilename = NULL;
       
  2192 	RBufReadStream readStream;
       
  2193 	readStream.Open( *iItemData );
       
  2194 	readStream.PushL();
       
  2195 
       
  2196     RPointerArray<CCalEntry> rdArray;
       
  2197 	CleanupStack::PushL( PtrArrCleanupItemRArr ( CCalEntry, &rdArray ) );
       
  2198 	
       
  2199     if( iIsHierarchicalSyncSupported )
       
  2200         {
       
  2201         //Get the Folder Information
       
  2202         TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL(iParentItemId) );
       
  2203         }
       
  2204     else
       
  2205         {
       
  2206         calfilename = iOpenedStoreName->AllocL();
       
  2207         }
       
  2208         
       
  2209     if( err != KErrNone || NULL == calfilename )
       
  2210         {
       
  2211         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL:Invalid CalendarInfo"));
       
  2212         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2213         User::Leave( KErrNotFound );
       
  2214         }
       
  2215         
       
  2216     agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  2217     if( agendautil )
       
  2218         {
       
  2219         CleanupStack::PushL(agendautil);
       
  2220         TRAP(err, agendautil->InitializeCalAPIsL( calfilename ));
       
  2221         CleanupStack::Pop(agendautil);
       
  2222         }   
       
  2223         
       
  2224     if ( err || !agendautil )
       
  2225         {
       
  2226         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL:Invalid CalendarInfo"));
       
  2227         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2228         delete calfilename;
       
  2229         User::Leave( KErrNotFound );
       
  2230         }
       
  2231         
       
  2232     if ( iRXEntryType == ENSmlICal )
       
  2233         {
       
  2234         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: ImportICalendarL"));           
       
  2235         agendautil->iImporter->ImportICalendarL( readStream, rdArray );
       
  2236         }
       
  2237     else if ( iRXEntryType == ENSmlVCal )
       
  2238         {
       
  2239         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: ImportVCalendarL"));
       
  2240         agendautil->iImporter->ImportVCalendarL( readStream, rdArray );
       
  2241         }
       
  2242     else
       
  2243         {
       
  2244         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2245         delete agendautil;
       
  2246         delete calfilename; 
       
  2247         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL - \
       
  2248                    KErrNotSupported: END"));
       
  2249         User::Leave( KErrNotSupported );
       
  2250         }
       
  2251     
       
  2252     // If rdArray is empty or there is multiple items then return error
       
  2253     // Multiple items are not supported
       
  2254     if ( rdArray.Count() != 1 )
       
  2255         {
       
  2256         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2257         delete agendautil;
       
  2258         delete calfilename; 
       
  2259         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL - \
       
  2260                    Multiple items are not supported: END"));
       
  2261         User::Leave( KErrNotSupported );
       
  2262         }           
       
  2263         
       
  2264     err = KErrNone;    
       
  2265     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: before StoreL"));
       
  2266     TRAP( err, iInterimUtils->StoreL( *agendautil->iEntryView, *rdArray[0], ETrue ) );
       
  2267     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: after StoreL '%d'"), err );
       
  2268     if ( err )
       
  2269         {
       
  2270         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2271         delete agendautil;
       
  2272         delete calfilename; 
       
  2273         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL - \
       
  2274                    Error at storing item to database: END"));
       
  2275         User::Leave( KErrGeneral );
       
  2276         }
       
  2277 
       
  2278     *iAddItemId = rdArray[0]->LocalUidL();        
       
  2279     CCalEntry* newEntry = agendautil->iEntryView->FetchL( *iAddItemId );
       
  2280     
       
  2281     // RD_MULTICAL
       
  2282     if( iIsHierarchicalSyncSupported )
       
  2283         {
       
  2284         *iAddItemId = *iAddItemId + iParentItemId;
       
  2285         }
       
  2286     // RD_MULTICAL
       
  2287     
       
  2288     if( newEntry )
       
  2289         {
       
  2290         CleanupStack::PushL( newEntry );
       
  2291         
       
  2292         replicationStatus = newEntry->ReplicationStatusL();
       
  2293     
       
  2294         if ( CanBeSynchronized( replicationStatus ) )
       
  2295             {
       
  2296             if ( iChangeFinder )
       
  2297                 {
       
  2298                 TNSmlSnapshotItem item( *iAddItemId );
       
  2299                     item.SetLastChangedDate(
       
  2300                     newEntry->LastModifiedDateL().TimeUtcL() );
       
  2301                 item.SetSoftDelete( EFalse );
       
  2302                 TRAPD( changeFinderError, iChangeFinder->ItemAddedL( item ) );
       
  2303                 if ( changeFinderError == KErrAlreadyExists )
       
  2304                     {
       
  2305                     iChangeFinder->ItemUpdatedL( item );
       
  2306                     }
       
  2307                 else
       
  2308                     {
       
  2309                     User::LeaveIfError( changeFinderError );    
       
  2310                     }
       
  2311                 }
       
  2312             }
       
  2313         CleanupStack::PopAndDestroy();// newEntry,
       
  2314         }
       
  2315 	CleanupStack::PopAndDestroy( 2 ); //  rdArray, readStream 
       
  2316     delete agendautil;
       
  2317 	delete calfilename;     	
       
  2318       
       
  2319     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateCalItemL: END"));
       
  2320     }
       
  2321     
       
  2322 // -----------------------------------------------------------------------------
       
  2323 // CNSmlAgendaDataStore::DoCommitReplaceCalItemL
       
  2324 // Commit Calendar item data to database when replacing item.
       
  2325 // -----------------------------------------------------------------------------
       
  2326 //
       
  2327 void CNSmlAgendaDataStore::DoCommitReplaceCalItemL()
       
  2328     {
       
  2329     FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: BEGIN"));
       
  2330     iState = ENSmlOpenAndWaiting; // iState set to closed to handle leave
       
  2331 	CBufFlat* oldItem = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
  2332 	CleanupStack::PushL( oldItem );
       
  2333 	RBufWriteStream writeStream( *oldItem );
       
  2334 	writeStream.PushL();
       
  2335 	CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  2336 	HBufC* calfilename = NULL;
       
  2337 	TInt err( KErrNone );
       
  2338 	
       
  2339     
       
  2340     if( iIsHierarchicalSyncSupported )
       
  2341         {
       
  2342         TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL(iParentItemId) );
       
  2343         }
       
  2344     else
       
  2345         {
       
  2346         calfilename = iOpenedStoreName->AllocL();
       
  2347         }
       
  2348     
       
  2349     if( err != KErrNone || NULL == calfilename )
       
  2350         {
       
  2351         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL:Invalid CalendarInfo"));
       
  2352         CleanupStack::PopAndDestroy( 2 ); // olditem, writeStream
       
  2353         User::Leave( KErrNotFound );
       
  2354         }
       
  2355     
       
  2356     FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: entry id: '%d'"), iReplaceItemId);
       
  2357     
       
  2358     agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  2359     if( agendautil )
       
  2360         {
       
  2361         CleanupStack::PushL(agendautil);
       
  2362         TRAP(err, agendautil->InitializeCalAPIsL( calfilename, iReplaceItemId ));
       
  2363         CleanupStack::Pop(agendautil);
       
  2364         }
       
  2365         
       
  2366     if ( err || !agendautil )
       
  2367         {
       
  2368         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: entry is not valid"));
       
  2369         CleanupStack::PopAndDestroy( 2 ); // olditem, writeStream
       
  2370         delete agendautil;
       
  2371         delete calfilename;
       
  2372         User::Leave( KErrGeneral );
       
  2373         }  
       
  2374         
       
  2375     // Export item from database depending on transmitted item entry type
       
  2376     if ( iTXEntryType == ENSmlVCal )
       
  2377         {
       
  2378         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: ExportVCalL"));
       
  2379         agendautil->iExporter->ExportVCalL( *agendautil->iEntry, writeStream );        
       
  2380         }
       
  2381 #ifdef __NSML_USE_ICAL_FEATURE
       
  2382     else if ( iTXEntryType == ENSmlICal )
       
  2383         {
       
  2384         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: ExportICalL"));
       
  2385         agendautil->iExporter->ExportICalL( *agendautil->iEntry, writeStream );
       
  2386         }
       
  2387 #endif // __NSML_USE_ICAL_FEATURE
       
  2388     else
       
  2389         {
       
  2390         CleanupStack::PopAndDestroy( 2 ); // olditem, writeStream
       
  2391         delete agendautil;
       
  2392         delete calfilename;
       
  2393         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL - \
       
  2394                    KErrNotSupported: END"));
       
  2395         User::Leave( KErrNotSupported );
       
  2396         }
       
  2397         
       
  2398     writeStream.CommitL();
       
  2399     oldItem->Compress();
       
  2400 
       
  2401     CleanupStack::PopAndDestroy( 1 ); 
       
  2402     
       
  2403     // Get original UID, geoId and Recurrence-ID properties
       
  2404     HBufC8* uid = NULL;
       
  2405     HBufC8* recurrenceId = NULL;
       
  2406     HBufC8* xRecurrenceId = NULL;
       
  2407     HBufC8* geoId = NULL;
       
  2408     GetPropertiesFromDataL( oldItem, uid, KVersitTokenUID()  );
       
  2409     GetPropertiesFromDataL( oldItem, recurrenceId, KNSmlVersitTokenRecurrenceID() );
       
  2410     GetPropertiesFromDataL( oldItem, xRecurrenceId, KNSmlVersitTokenXRecurrenceID() );
       
  2411     GetPropertiesFromDataL( oldItem, geoId, KNSmlVersitTokenGeoID() );
       
  2412     CleanupStack::PushL( uid );
       
  2413     CleanupStack::PushL( recurrenceId );
       
  2414     CleanupStack::PushL( xRecurrenceId );
       
  2415     CleanupStack::PushL( geoId );
       
  2416 
       
  2417     if ( iDataMod->NeedsMerge() )
       
  2418         {
       
  2419         // Merge data
       
  2420         iDataMod->MergeRxL( *iItemData, *oldItem );
       
  2421         }
       
  2422     
       
  2423     // Add original UID and Recurrence-ID to merged data
       
  2424     // This first removes UID and Recurrence-ID from merged data
       
  2425     // and then adds original ones
       
  2426     if ( uid )
       
  2427         {
       
  2428         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: SetPropertiesToDataL :uid"));
       
  2429         SetPropertiesToDataL( uid, KVersitTokenUID() );    
       
  2430         }
       
  2431     else
       
  2432         {
       
  2433         CleanupStack::PopAndDestroy( 5 ); // xRecurrenceId, recurrenceId,
       
  2434                                               // uid, oldItem, geoId
       
  2435         delete agendautil;
       
  2436         delete calfilename;
       
  2437         
       
  2438         User::Leave( KErrNotSupported );
       
  2439         }
       
  2440     if ( recurrenceId )
       
  2441         {
       
  2442         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: SetPropertiesToDataL :recurrenceId"));
       
  2443         SetPropertiesToDataL( recurrenceId, KNSmlVersitTokenRecurrenceID() );    
       
  2444         }
       
  2445     if ( xRecurrenceId )
       
  2446         {
       
  2447         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: SetPropertiesToDataL :xRecurrenceId"));
       
  2448         SetPropertiesToDataL( xRecurrenceId, KNSmlVersitTokenXRecurrenceID() );    
       
  2449         }
       
  2450     if ( geoId )
       
  2451         {
       
  2452         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: SetPropertiesToDataL :geoId"));
       
  2453         SetPropertiesToDataL( geoId, KNSmlVersitTokenGeoID() );    
       
  2454         }
       
  2455     
       
  2456     CleanupStack::PopAndDestroy( 5 ); // xRecurrenceId, recurrenceId,
       
  2457                                       // uid, oldItem, geoId
       
  2458     
       
  2459     // Replace item to database
       
  2460     RBufReadStream readStream;
       
  2461     readStream.Open( *iItemData );
       
  2462     readStream.PushL();
       
  2463 
       
  2464     RPointerArray<CCalEntry> rdArray;
       
  2465     CleanupStack::PushL( PtrArrCleanupItemRArr ( CCalEntry, &rdArray ) );
       
  2466 
       
  2467     // Import item to database depending on received item entry type
       
  2468     if ( iRXEntryType == ENSmlVCal )
       
  2469         {
       
  2470         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: ImportVCalendarL"));
       
  2471         TRAP( err, agendautil->iImporter->ImportVCalendarL( readStream, rdArray ));
       
  2472         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: ImportVCalendarL error: '%d'"), err);
       
  2473         if( err != KErrNone)
       
  2474             {
       
  2475             User::Leave(err);
       
  2476             }
       
  2477         }
       
  2478 #ifdef __NSML_USE_ICAL_FEATURE
       
  2479     else if ( iRXEntryType == ENSmlICal )
       
  2480         {
       
  2481         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: ImportICalendarL"));
       
  2482         agendautil->iImporter->ImportICalendarL( readStream, rdArray );
       
  2483         }
       
  2484 #endif // __NSML_USE_ICAL_FEATURE
       
  2485     else
       
  2486         {
       
  2487         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2488         delete agendautil;
       
  2489         delete calfilename;
       
  2490         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceItemL - \
       
  2491                    KErrNotSupported: END"));
       
  2492         User::Leave( KErrNotSupported );
       
  2493         }
       
  2494 
       
  2495     // If rdArray is empty or there is multiple items then return error
       
  2496     // Multiple items are not supported
       
  2497     if ( rdArray.Count() != 1 )
       
  2498         {
       
  2499         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: Multiple items are not supported "));
       
  2500         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2501         delete agendautil;
       
  2502         delete calfilename;
       
  2503         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceItemL - \
       
  2504                    Multiple items are not supported: END"));
       
  2505         User::Leave( KErrNotSupported );
       
  2506         }
       
  2507         
       
  2508     err = KErrNone;
       
  2509 
       
  2510     FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: before StoreL"));
       
  2511     TRAP( err, iInterimUtils->StoreL( *agendautil->iEntryView, *rdArray[0], ETrue ) );
       
  2512     FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: after StoreL '%d'"), err );
       
  2513     
       
  2514     if ( err )
       
  2515         {
       
  2516         CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2517         delete agendautil;
       
  2518         delete calfilename;
       
  2519         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL - \
       
  2520                    Error at storing item to database: END"));
       
  2521         User::Leave( KErrGeneral );
       
  2522         }
       
  2523 
       
  2524     CCalEntry::TReplicationStatus  replicationStatus;
       
  2525     
       
  2526     CCalEntry* replacedEntry = agendautil->iEntryView->FetchL( iReplaceItemId );
       
  2527     if( replacedEntry )
       
  2528     {
       
  2529     CleanupStack::PushL( replacedEntry );
       
  2530     
       
  2531     replicationStatus = replacedEntry->ReplicationStatusL();
       
  2532     
       
  2533     // RD_MULTICAL
       
  2534     if( !iIsHierarchicalSyncSupported )
       
  2535         {
       
  2536         iParentItemId = 0;
       
  2537         }
       
  2538     // RD_MULTICAL
       
  2539                 
       
  2540     if ( CanBeSynchronized( replicationStatus ) )
       
  2541         {
       
  2542         if ( iChangeFinder )
       
  2543             {
       
  2544             TNSmlSnapshotItem item( iReplaceItemId + iParentItemId );
       
  2545             item.SetLastChangedDate(
       
  2546                         replacedEntry->LastModifiedDateL().TimeUtcL());
       
  2547             item.SetSoftDelete( EFalse );
       
  2548             iChangeFinder->ItemUpdatedL( item );
       
  2549             }
       
  2550         }
       
  2551     CleanupStack::PopAndDestroy(); // replacedEntry
       
  2552     }
       
  2553     
       
  2554 	CleanupStack::PopAndDestroy( 2 ); // rdArray, readStream
       
  2555     delete agendautil;
       
  2556 	delete calfilename;	
       
  2557 	FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceCalItemL: END"));
       
  2558     }
       
  2559 
       
  2560 // -----------------------------------------------------------------------------
       
  2561 // CNSmlAgendaDataStore::DoCommitCreateFolderItemL
       
  2562 // Commit Folder item data to database when adding item.
       
  2563 // -----------------------------------------------------------------------------
       
  2564 //
       
  2565 void CNSmlAgendaDataStore::DoCommitCreateFolderItemL()
       
  2566     {
       
  2567     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateFolderItemL: BEGIN"));
       
  2568     
       
  2569     TInt err(KErrNone);
       
  2570     RBufReadStream readStream;
       
  2571     readStream.Open( *iItemData );
       
  2572     readStream.PushL();
       
  2573     
       
  2574     TRAP(err, *iAddItemId = iAgendaAdapterHandler->CreateFolderL( readStream ));
       
  2575     if( err != KErrNone )
       
  2576         {
       
  2577         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateFolderItemL: Error in Create Folder"));
       
  2578         CleanupStack::PopAndDestroy( &readStream );
       
  2579         User::Leave( err );    
       
  2580         }
       
  2581    
       
  2582     // Add snapshotitem
       
  2583     if ( iChangeFinder )
       
  2584         {
       
  2585         FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateFolderItemL: item id: '%d'"), *iAddItemId );
       
  2586         TNSmlSnapshotItem item( *iAddItemId );
       
  2587         TTime time;
       
  2588         time.HomeTime();
       
  2589         item.SetLastChangedDate( time );
       
  2590         item.SetSoftDelete( EFalse );
       
  2591         TRAPD( changeFinderError, iChangeFinder->ItemAddedL( item ) );
       
  2592         if ( changeFinderError == KErrAlreadyExists )
       
  2593             {
       
  2594             iChangeFinder->ItemUpdatedL( item );
       
  2595             }
       
  2596         else
       
  2597             {
       
  2598             User::LeaveIfError( changeFinderError );    
       
  2599             }
       
  2600         
       
  2601         iCalOffsetArr->AppendL(*iAddItemId);
       
  2602             
       
  2603         }
       
  2604     
       
  2605     CleanupStack::PopAndDestroy( &readStream );
       
  2606     
       
  2607     FLOG(_L("CNSmlAgendaDataStore::DoCommitCreateFolderItemL: END"));
       
  2608     }
       
  2609     
       
  2610 
       
  2611 // -----------------------------------------------------------------------------
       
  2612 // CNSmlAgendaDataStore::DoCommitReplaceFolderItemL
       
  2613 // Commit Folder item data to database when replacing item.
       
  2614 // -----------------------------------------------------------------------------
       
  2615 //
       
  2616 void CNSmlAgendaDataStore::DoCommitReplaceFolderItemL()
       
  2617     {
       
  2618     FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceFolderItemL: BEGIN"));
       
  2619     
       
  2620     TBool syncstatus(ETrue);
       
  2621     TInt err(KErrNone);
       
  2622     RBufReadStream readStream;
       
  2623     readStream.Open( *iItemData );
       
  2624     readStream.PushL();
       
  2625     
       
  2626     TRAP(err, iAgendaAdapterHandler->ReplaceFolderL( iReplaceItemId, readStream, syncstatus ));
       
  2627     if(err != KErrNone)
       
  2628         {
       
  2629         FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceFolderItemL: Error while replacing Folder"));
       
  2630         CleanupStack::PopAndDestroy( &readStream );
       
  2631         User::Leave(err);
       
  2632         }
       
  2633    
       
  2634     // Update snapshotitem
       
  2635     if ( iChangeFinder && syncstatus )
       
  2636         {
       
  2637         TNSmlSnapshotItem item( iReplaceItemId );
       
  2638         TTime time;
       
  2639         time.HomeTime();
       
  2640         item.SetLastChangedDate(time);
       
  2641         item.SetSoftDelete( EFalse );
       
  2642         iChangeFinder->ItemUpdatedL( item );
       
  2643         }
       
  2644         
       
  2645     CleanupStack::PopAndDestroy( &readStream );    
       
  2646     FLOG(_L("CNSmlAgendaDataStore::DoCommitReplaceFolderItemL: END"));
       
  2647     }
       
  2648 // -----------------------------------------------------------------------------
       
  2649 // CNSmlAgendaDataStore::GetPropertiesFromDataL
       
  2650 // Gets property from old item.
       
  2651 // -----------------------------------------------------------------------------
       
  2652 //
       
  2653 void CNSmlAgendaDataStore::GetPropertiesFromDataL( CBufFlat* aOldItem,
       
  2654                                                    HBufC8*& aValue,
       
  2655 	                                               const TDesC8& aProperty )
       
  2656     {
       
  2657     FLOG(_L("CNSmlAgendaDataStore::GetPropertiesFromDataL(): begin"));
       
  2658     
       
  2659     // Gemerate property that is searched (Linebreak + property + tokencolon)
       
  2660     HBufC8* startBuffer = HBufC8::NewLC( KVersitTokenCRLF().Size() +
       
  2661                                          aProperty.Length() );
       
  2662     TPtr8 startBufferPtr( startBuffer->Des() );
       
  2663 	startBufferPtr.Append( KVersitTokenCRLF() );
       
  2664 	startBufferPtr.Append( aProperty );
       
  2665 	
       
  2666 	// Get start position of property value
       
  2667 	TInt startPos = aOldItem->Ptr( 0 ).Find( startBufferPtr );
       
  2668 	
       
  2669 	TInt endPos( KErrNotFound );
       
  2670 	if ( startPos != KErrNotFound )
       
  2671 	    {
       
  2672 	    // startPos is before UID word
       
  2673 	    startPos = startPos + KVersitTokenCRLF().Length();
       
  2674 	    
       
  2675 	    // End end position of property value
       
  2676 	    endPos = startPos + 
       
  2677 	             aOldItem->Ptr( startPos ).Find( KVersitTokenCRLF() );
       
  2678 	    
       
  2679 	    TPtrC8 tempPtr( aOldItem->Ptr( 0 ).Mid( endPos ) );
       
  2680 	    
       
  2681 	    // If next char after linebreak is ' ' or '=' then it is only
       
  2682 	    // linebreak inside of value. This does not work with base64 coding!
       
  2683 	    while ( tempPtr[KVersitTokenCRLF().Length()] == ' ' ||
       
  2684 	            tempPtr[KVersitTokenCRLF().Length()] == '=' )
       
  2685 	        {
       
  2686 	        endPos = endPos + KVersitTokenCRLF().Length();
       
  2687 	        TInt tempPos = aOldItem->Ptr( endPos ).Find( KVersitTokenCRLF() );
       
  2688 	        endPos = endPos + tempPos;
       
  2689 	        tempPtr.Set( aOldItem->Ptr( 0 ).Mid( endPos ) );
       
  2690 	        }
       
  2691         
       
  2692         // Then cut value to value parameter
       
  2693         delete aValue;
       
  2694 		aValue = NULL;
       
  2695 		TInt length = endPos - startPos;
       
  2696 		aValue = HBufC8::NewLC( length );
       
  2697 		TPtr8 valuePtr( aValue->Des() );
       
  2698 		aOldItem->Read( startPos, valuePtr, length );
       
  2699 		CleanupStack::Pop( aValue ); // aValue
       
  2700 	    }
       
  2701     CleanupStack::PopAndDestroy( startBuffer ); // startBuffer
       
  2702     
       
  2703     FLOG(_L("CNSmlAgendaDataStore::GetPropertiesFromDataL(): end"));
       
  2704     }
       
  2705   
       
  2706 // -----------------------------------------------------------------------------
       
  2707 // Adds property and it's value to new item.
       
  2708 // This method also removes original property and it's value.
       
  2709 // -----------------------------------------------------------------------------
       
  2710 //  
       
  2711 void CNSmlAgendaDataStore::SetPropertiesToDataL( HBufC8*& aValue,
       
  2712 	                                             const TDesC8& aProperty )
       
  2713     {
       
  2714     FLOG(_L("CNSmlAgendaDataStore::SetPropertiesToDataL(): begin"));
       
  2715     
       
  2716     // Gemerate property that is searched (Linebreak + property + tokencolon)
       
  2717     HBufC8* startBuffer = HBufC8::NewLC( KVersitTokenCRLF().Size() +
       
  2718                                          aProperty.Length() );
       
  2719     TPtr8 startBufferPtr( startBuffer->Des() );
       
  2720 	startBufferPtr.Append( KVersitTokenCRLF() );
       
  2721 	startBufferPtr.Append( aProperty );
       
  2722 	
       
  2723 	// Get start position of property value
       
  2724 	TInt startPos = iItemData->Ptr( 0 ).Find( startBufferPtr );
       
  2725 	
       
  2726 	TInt endPos( KErrNotFound );
       
  2727 	if ( startPos != KErrNotFound )
       
  2728 	    {
       
  2729 	    // startPos is before UID word
       
  2730 	    startPos = startPos + KVersitTokenCRLF().Length();
       
  2731 	    
       
  2732 	    // End end position of property value
       
  2733 	    endPos = startPos + 
       
  2734 	             iItemData->Ptr( startPos ).Find( KVersitTokenCRLF() );
       
  2735 	    
       
  2736 	    TPtrC8 tempPtr( iItemData->Ptr( 0 ).Mid( endPos ) );
       
  2737 	    
       
  2738 	    // If next char after linebreak is ' ' or '=' then it is only
       
  2739 	    // linebreak inside of value. This does not work with base64 coding!
       
  2740 	    while ( tempPtr[KVersitTokenCRLF().Length()] == ' ' ||
       
  2741 	            tempPtr[KVersitTokenCRLF().Length()] == '=' )
       
  2742 	        {
       
  2743 	        endPos = endPos + KVersitTokenCRLF().Length();
       
  2744 	        TInt tempPos = iItemData->Ptr( endPos ).Find( KVersitTokenCRLF() );
       
  2745 	        endPos = endPos + tempPos;
       
  2746 	        tempPtr.Set( iItemData->Ptr( 0 ).Mid( endPos ) );
       
  2747 	        }
       
  2748         
       
  2749         // Delete original property and value
       
  2750         iItemData->Delete( startPos, endPos - startPos );
       
  2751         
       
  2752 	    }
       
  2753 
       
  2754     // Add property and new value from parameter.
       
  2755     // First find end of VEVENT or VTODO
       
  2756     
       
  2757     // Generate VEVENT END property
       
  2758     HBufC8* endVEvent = HBufC8::NewLC( KVersitTokenCRLF().Size() + 
       
  2759                                      KVersitTokenEND().Length() +
       
  2760                                      KVersitTokenColon().Length() +
       
  2761                                      KVersitVarTokenVEVENT().Length() );
       
  2762 	TPtr8 endVEventPtr( endVEvent->Des() );
       
  2763 	endVEventPtr.Append( KVersitTokenCRLF() );
       
  2764 	endVEventPtr.Append( KVersitTokenEND() );
       
  2765 	endVEventPtr.Append( KVersitTokenColon() );
       
  2766 	endVEventPtr.Append( KVersitVarTokenVEVENT() );
       
  2767 	
       
  2768 	// Generate VTODO END property
       
  2769     HBufC8* endVTodo = HBufC8::NewLC( KVersitTokenCRLF().Size() + 
       
  2770                                      KVersitTokenEND().Length() +
       
  2771                                      KVersitTokenColon().Length() +
       
  2772                                      KVersitVarTokenVTODO().Length() );
       
  2773 	TPtr8 endVTodoPtr( endVTodo->Des() );
       
  2774 	endVTodoPtr.Append( KVersitTokenCRLF() );
       
  2775 	endVTodoPtr.Append( KVersitTokenEND() );
       
  2776 	endVTodoPtr.Append( KVersitTokenColon() );
       
  2777 	endVTodoPtr.Append( KVersitVarTokenVTODO() );
       
  2778     
       
  2779     // Find end of VEVENT or VTODO
       
  2780     endPos = iItemData->Ptr( 0 ).Find( endVEventPtr );
       
  2781     if ( endPos == KErrNotFound )
       
  2782         {
       
  2783         endPos = iItemData->Ptr( 0 ).Find( endVTodoPtr );
       
  2784         if ( endPos == KErrNotFound )
       
  2785             {
       
  2786             User::Leave( KErrNotFound );
       
  2787             }
       
  2788         }
       
  2789 
       
  2790     // Add property and value from parameter
       
  2791 	iItemData->InsertL( endPos, KVersitTokenCRLF() );
       
  2792 	iItemData->InsertL( endPos + KVersitTokenCRLF().Size(),
       
  2793 	                    aValue->Des() );
       
  2794     iItemData->Compress();
       
  2795     
       
  2796     CleanupStack::PopAndDestroy( 3 ); // endVTodo, endVEvent, startBuffer
       
  2797     
       
  2798     FLOG(_L("CNSmlAgendaDataStore::SetPropertiesToDataL(): end"));
       
  2799     }
       
  2800 
       
  2801 // -----------------------------------------------------------------------------
       
  2802 // CNSmlAgendaDataStore::GetCalendarEntryIdL
       
  2803 // Method to retrieve the Id of the Calendar Entry
       
  2804 // -----------------------------------------------------------------------------
       
  2805 void CNSmlAgendaDataStore::GetCalendarEntryIdL( TCalLocalUid& aParentId, TCalLocalUid& aCalId ) const
       
  2806     {
       
  2807     FLOG(_L("CNSmlAgendaDataStore::GetCalendarEntryIdL: BEGIN"));
       
  2808     
       
  2809     FLOG(_L("CNSmlAgendaDataStore::GetCalendarEntryIdL: parentid: '%d'"), aParentId);
       
  2810     FLOG(_L("CNSmlAgendaDataStore::GetCalendarEntryIdL: aCalId: '%d'"), aCalId);
       
  2811     
       
  2812     TInt uidcalentry = aCalId;
       
  2813     
       
  2814     if( uidcalentry < iCalOffsetVal )
       
  2815         {
       
  2816         FLOG(_L("CNSmlAgendaDataStore::GetCalendarEntryIdL: Invalid CalendarId: '%d'"), aCalId);
       
  2817         User::Leave( KErrGeneral );
       
  2818         }
       
  2819     else if( uidcalentry == iCalOffsetVal ||
       
  2820              0 == (uidcalentry % iCalOffsetVal) )
       
  2821         {
       
  2822         aParentId = uidcalentry;
       
  2823         }
       
  2824     else
       
  2825         {
       
  2826         while( uidcalentry > iCalOffsetVal )
       
  2827             {
       
  2828             uidcalentry = uidcalentry - iCalOffsetVal;
       
  2829             }    
       
  2830         aParentId = aCalId - uidcalentry;
       
  2831         aCalId = uidcalentry;
       
  2832         }
       
  2833     
       
  2834     TInt err(KErrNone);
       
  2835     TInt index = KErrNotFound;
       
  2836     TKeyArrayFix key( 0, ECmpTInt ); // Find key for Ids.
       
  2837     
       
  2838     err = iCalOffsetArr->Find( aParentId, key, index );
       
  2839     
       
  2840     if( err != KErrNone )
       
  2841         {
       
  2842         User::Leave(err);
       
  2843         }   
       
  2844     FLOG(_L("CNSmlAgendaDataStore::GetCalendarEntryIdL: END"));    
       
  2845     }
       
  2846 
       
  2847 // -----------------------------------------------------------------------------
       
  2848 // CNSmlAgendaDataStore::DataMimeType
       
  2849 // Method to determine the MIME type, provided the UID
       
  2850 // -----------------------------------------------------------------------------
       
  2851 void CNSmlAgendaDataStore::DataMimeType( TSmlDbItemUid aUid )
       
  2852     {
       
  2853     FLOG(_L("CNSmlAgendaDataStore::DataMimeType: BEGIN"));    
       
  2854     
       
  2855     FLOG(_L("CNSmlAgendaDataStore::DataMimeType: LUID '%d'"), aUid );
       
  2856     
       
  2857     if( iIsHierarchicalSyncSupported )
       
  2858         {
       
  2859         // Find a match with the list of Offset values available
       
  2860         TInt index = KErrNotFound;
       
  2861         TInt err = KErrNone;
       
  2862         TKeyArrayFix key( 0, ECmpTInt ); // Find key for Ids.
       
  2863         
       
  2864         err = iCalOffsetArr->Find( aUid, key, index );
       
  2865                    
       
  2866         // Determine the Data Mime Type
       
  2867         if( err == KErrNone )
       
  2868             {
       
  2869             iDataMimeType = ENSmlFolder;
       
  2870             }
       
  2871         else
       
  2872             {
       
  2873             iDataMimeType = ENSmlCalendar;
       
  2874             }
       
  2875         }
       
  2876     else
       
  2877         {
       
  2878         iDataMimeType = ENSmlCalendar;
       
  2879         }
       
  2880     
       
  2881     FLOG(_L("CNSmlAgendaDataStore::DataMimeType: END, DataMimeType '%d' "), iDataMimeType);    
       
  2882     }
       
  2883 
       
  2884 // -----------------------------------------------------------------------------
       
  2885 // CNSmlAgendaDataStore::ListAllAgendaPluginAdaptersL
       
  2886 // Get All the AdapterHandler instance
       
  2887 // -----------------------------------------------------------------------------
       
  2888 void CNSmlAgendaDataStore::ListAllAgendaPluginAdaptersL()
       
  2889     {
       
  2890     FLOG(_L("CNSmlAgendaDataStore::ListAllAgendaPluginAdaptersL BEGIN"));
       
  2891     
       
  2892     RImplInfoPtrArray adapterInfoArray;        
       
  2893     CNSmlAgendaAdapterHandler* adapterInstance(NULL);
       
  2894     
       
  2895     REComSession::ListImplementationsL( KAgendaAdapterHandlerInterfaceUid, adapterInfoArray );
       
  2896     TInt adapterCount = adapterInfoArray.Count();
       
  2897     FLOG(_L("adaptercount:'%d'"),adapterCount); 
       
  2898     CImplementationInformation* adapterInfo = NULL;
       
  2899     
       
  2900     for (TInt adapterIndex = 0 ; adapterIndex < adapterCount; adapterIndex++)
       
  2901         {
       
  2902         adapterInfo = adapterInfoArray[ adapterIndex ];
       
  2903         TUid adapterUid = { adapterInfo->ImplementationUid().iUid  };
       
  2904         FLOG(_L("CNSmlAgendaDataStore::ListAllAgendaPluginAdaptersL before newl"));
       
  2905         adapterInstance = CNSmlAgendaAdapterHandler::NewL(adapterUid); 
       
  2906         FLOG(_L("CNSmlAgendaDataStore::ListAllAgendaPluginAdaptersL after newl"));
       
  2907         if( adapterInstance )
       
  2908             {
       
  2909             adapterInstance->iOpaqueData = adapterInfo->OpaqueData().Alloc();
       
  2910             iAgendaPluginAdapters.AppendL( adapterInstance );
       
  2911             }
       
  2912         }
       
  2913     adapterInfoArray.ResetAndDestroy();
       
  2914     FLOG(_L("CNSmlAgendaDataStore::ListAllAgendaPluginAdaptersL END"));
       
  2915     }
       
  2916 
       
  2917 // -----------------------------------------------------------------------------
       
  2918 // CNSmlAgendaDataStore::AgendaAdapterHandlerL
       
  2919 // Get the AdapterHandler instance
       
  2920 // -----------------------------------------------------------------------------
       
  2921 void CNSmlAgendaDataStore::AgendaAdapterHandlerL()
       
  2922     {
       
  2923     FLOG(_L("CNSmlAgendaDataStore::AgendaAdapterHandlerL BEGIN"));
       
  2924     
       
  2925     // Get the Opaque data / server id from the cenrep
       
  2926     TBuf<KBuffLength> serverid;
       
  2927     HBufC8* opaquedata;
       
  2928     CRepository* rep = CRepository::NewLC( KNsmlDsSessionInfoKey );
       
  2929     TInt err = rep->Get(EDSSessionServerId, serverid );
       
  2930     FLOG(_L("CNSmlAgendaDataStore:::AgendaAdapterHandlerL():serverid '%S'"), &serverid );
       
  2931     User::LeaveIfError(err);
       
  2932     CleanupStack::PopAndDestroy(rep);
       
  2933     
       
  2934     if( 0 == serverid.Length() )
       
  2935         {
       
  2936         FLOG(_L("CNSmlAgendaDataStore:::AgendaAdapterHandlerL():serverid length is zero"));
       
  2937         opaquedata = KNSmlDefaultOpaqueData().AllocL();
       
  2938         }
       
  2939     else
       
  2940         {
       
  2941         opaquedata = HBufC8::NewL( serverid.Length() );
       
  2942         TPtr8 name( opaquedata->Des() );
       
  2943         CnvUtfConverter::ConvertFromUnicodeToUtf8(name , serverid );
       
  2944         }
       
  2945     iAgendaAdapterHandler = NULL;
       
  2946     for( TInt adaptercount = 0; adaptercount < iAgendaPluginAdapters.Count(); adaptercount++ )
       
  2947         {
       
  2948         if( 0 == opaquedata->Compare(iAgendaPluginAdapters[adaptercount]->iOpaqueData->Des()) )
       
  2949             {
       
  2950             iAgendaAdapterHandler = iAgendaPluginAdapters[adaptercount];
       
  2951             break;
       
  2952             }        
       
  2953         }
       
  2954     
       
  2955     delete opaquedata;
       
  2956     
       
  2957     if( NULL == iAgendaAdapterHandler )
       
  2958         {
       
  2959         opaquedata = KNSmlDefaultOpaqueData().AllocL();
       
  2960         for( TInt adaptercount = 0; adaptercount < iAgendaPluginAdapters.Count(); adaptercount++ )
       
  2961             {
       
  2962             if( 0 == opaquedata->Compare(iAgendaPluginAdapters[adaptercount]->iOpaqueData->Des()) )
       
  2963                 {
       
  2964                 iAgendaAdapterHandler = iAgendaPluginAdapters[adaptercount];
       
  2965                 break;
       
  2966                 }        
       
  2967             }
       
  2968         delete opaquedata;
       
  2969         }
       
  2970     
       
  2971     FLOG(_L("CNSmlAgendaDataStore::AgendaAdapterHandlerL END"));
       
  2972     }
       
  2973 
       
  2974 // -----------------------------------------------------------------------------
       
  2975 // CNSmlAgendaDataStore::SynchronizableCalEntryIdsL
       
  2976 // Retrieve the CalendarFile and associated entries ID
       
  2977 // -----------------------------------------------------------------------------
       
  2978 void CNSmlAgendaDataStore::SynchronizableCalEntryIdsL( RArray<TCalLocalUid>& aUidArray )
       
  2979     {
       
  2980     FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL BEGIN"));
       
  2981     
       
  2982     TInt err(KErrNone);
       
  2983     
       
  2984     for( TInt calfilecount = 0; calfilecount < iCalOffsetArr->Count(); calfilecount++ )
       
  2985         {
       
  2986         TUint calfileid = iCalOffsetArr->At(calfilecount);
       
  2987         CNSmlAgendaDataStoreUtil* agendautil = NULL;
       
  2988         HBufC* calfilename = NULL;
       
  2989         
       
  2990         TRAP(err, calfilename = iAgendaAdapterHandler->FolderNameL(calfileid) );
       
  2991         FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL calfilename: '%S'"), calfilename);
       
  2992            
       
  2993         if( err!= KErrNone || NULL == calfilename)
       
  2994             {
       
  2995             FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL Invalid FileName"));
       
  2996             User::Leave(KErrGeneral);
       
  2997             }
       
  2998         FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL Initialize"));
       
  2999        
       
  3000         agendautil = CNSmlAgendaDataStoreUtil::NewL();
       
  3001         if( agendautil )
       
  3002             {
       
  3003             CleanupStack::PushL(agendautil);
       
  3004             TRAP(err, agendautil->InitializeCalAPIsL( calfilename ));
       
  3005             CleanupStack::Pop(agendautil);
       
  3006             }
       
  3007         if ( err || !agendautil )
       
  3008             {
       
  3009             FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL Initialization failed"));
       
  3010             delete calfilename;
       
  3011             User::Leave(KErrGeneral);
       
  3012             }
       
  3013         
       
  3014         TCalTime zeroTime;
       
  3015         RArray<TCalLocalUid> entryarray;
       
  3016         CleanupClosePushL( entryarray );
       
  3017        
       
  3018         aUidArray.AppendL(calfileid);            
       
  3019     
       
  3020         zeroTime.SetTimeUtcL( Time::NullTTime() );
       
  3021         agendautil->iEntryView->GetIdsModifiedSinceDateL( zeroTime, entryarray );  
       
  3022         
       
  3023         for( TInt i=0; i<entryarray.Count(); i++)
       
  3024             {
       
  3025             aUidArray.AppendL(calfileid + entryarray[i]);
       
  3026             FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL, Entry ID: '%d'"), (calfileid + entryarray[i]));
       
  3027             }    
       
  3028         FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL Clean up start"));
       
  3029         CleanupStack::PopAndDestroy( &entryarray );
       
  3030         delete agendautil;
       
  3031         delete calfilename;
       
  3032         FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL Cleanup end"));
       
  3033         }    
       
  3034     FLOG(_L("CNSmlAgendaDataStore::SynchronizableCalEntryIdsL END"));
       
  3035     }
       
  3036 
       
  3037 // -----------------------------------------------------------------------------
       
  3038 // CNSmlAgendaDataStore::StoreFormatL
       
  3039 // Providing DataStore access to CNSmlDataProvider Class
       
  3040 // -----------------------------------------------------------------------------
       
  3041 CSmlDataStoreFormat* CNSmlAgendaDataStore::StoreFormatL()
       
  3042     {
       
  3043     return DoOwnStoreFormatL();
       
  3044     }
       
  3045 
       
  3046 // -----------------------------------------------------------------------------
       
  3047 // CNSmlAgendaDataStore::OpenStoreL
       
  3048 // Open the Store if present else create one to work upon
       
  3049 // -----------------------------------------------------------------------------
       
  3050 void CNSmlAgendaDataStore::OpenStoreL()
       
  3051     {
       
  3052     FLOG(_L("CNSmlAgendaDataStore::OpenStoreL BEGIN"));
       
  3053 
       
  3054     TInt profileid = NULL;
       
  3055     TBuf<KBuffLength> serverid;
       
  3056     TBuf<KBuffLength> profilename;
       
  3057 	TBool isHandlerAvailable( EFalse );
       
  3058     
       
  3059     // Get the ServerId, ProfileId and ProfileName from the cenrep
       
  3060     CRepository* rep = CRepository::NewLC( KNsmlDsSessionInfoKey );
       
  3061     TInt err = rep->Get( EDSSessionProfileId, profileid );
       
  3062     err = rep->Get( EDSSessionProfileName, profilename );
       
  3063     err = rep->Get(EDSSessionServerId, serverid );
       
  3064     User::LeaveIfError(err);
       
  3065     CleanupStack::PopAndDestroy(rep);
       
  3066     
       
  3067     // Reintialize the StoreName
       
  3068     if ( iOpenedStoreName )
       
  3069         {
       
  3070         delete iOpenedStoreName;
       
  3071         iOpenedStoreName = NULL;
       
  3072         }
       
  3073     
       
  3074     // Find the CalendarFile having the given ProfileID
       
  3075     CDesCArray* calfilearr = new (ELeave) CDesCArrayFlat(1);
       
  3076     CleanupStack::PushL(calfilearr);
       
  3077     FLOG(_L("CNSmlAgendaDataStore:::OpenStoreL():Profilename '%S'"), &profilename );
       
  3078     FLOG(_L("CNSmlAgendaDataStore:::OpenStoreL():serverid '%S'"), &serverid );
       
  3079 	
       
  3080 	if( 0 != serverid.Length() )
       
  3081         {   
       
  3082 		HBufC8* opaquedata = HBufC8::NewL( serverid.Length() );
       
  3083         TPtr8 name( opaquedata->Des() );
       
  3084         CnvUtfConverter::ConvertFromUnicodeToUtf8(name , serverid );
       
  3085 		
       
  3086 		for( TInt adaptercount = 0; adaptercount < iAgendaPluginAdapters.Count(); adaptercount++ )
       
  3087 	        {
       
  3088 	        if( 0 == opaquedata->Compare(iAgendaPluginAdapters[adaptercount]->iOpaqueData->Des()) )
       
  3089 	            {
       
  3090 	            FLOG(_L("CNSmlAgendaDataStore::Handler available"));			
       
  3091 				isHandlerAvailable = ETrue;
       
  3092 	            break;
       
  3093 	            }        
       
  3094 	        }
       
  3095 			delete opaquedata;
       
  3096         }	
       
  3097     
       
  3098 	if( isHandlerAvailable )
       
  3099 		{
       
  3100 		iOpenedStoreName = iDefaultStoreFileName->AllocL();
       
  3101 		}	
       
  3102     else if( IsCalFileAvailableL( profileid, calfilearr ) )
       
  3103         {
       
  3104         FLOG(_L("CNSmlAgendaDataStore::OpenStoreL Found the assoicated calfile"));
       
  3105         
       
  3106         // TODO: Yet to get clarification for enable/disable the notification
       
  3107         iOpenedStoreName = calfilearr->MdcaPoint(0).AllocL();
       
  3108         }
       
  3109     else
       
  3110         {
       
  3111         FLOG(_L("CNSmlAgendaDataStore::OpenStoreL CouldNot Find the Calfile"));
       
  3112         
       
  3113         // No association found thus creating a CalendarFile
       
  3114         HBufC* name = profilename.AllocL();
       
  3115         HBufC* calfilename = CreateCalFileL( name, profileid );
       
  3116         delete name;
       
  3117         iOpenedStoreName = calfilename;
       
  3118         }
       
  3119         
       
  3120     CCalSession* calsession = CCalSession::NewL();
       
  3121     CleanupStack::PushL(calsession);
       
  3122     TRAP( err, calsession->OpenL( iOpenedStoreName->Des() ) );
       
  3123     // Disable notifications
       
  3124     TRAP_IGNORE( calsession->DisablePubSubNotificationsL() );
       
  3125     TRAP_IGNORE( calsession->DisableChangeBroadcast() );        
       
  3126     // Get ID of database
       
  3127     calsession->FileIdL( iOpenedStoreId );
       
  3128     CleanupStack::PopAndDestroy( calsession );    
       
  3129       
       
  3130     CleanupStack::PopAndDestroy(calfilearr);
       
  3131     FLOG(_L("CNSmlAgendaDataStore::OpenStoreL END"));    
       
  3132     }
       
  3133 
       
  3134 // -----------------------------------------------------------------------------
       
  3135 // CNSmlAgendaDataStore::IsCalFileAvailableL    
       
  3136 // Check existance of CalFile with the given ProfileID associativity
       
  3137 // -----------------------------------------------------------------------------
       
  3138 TBool CNSmlAgendaDataStore::IsCalFileAvailableL( TInt aProfileId, CDesCArray* aCalFileArr )
       
  3139     {
       
  3140     FLOG(_L("CNSmlAgendaDataStore::IsCalFileAvailable: BEGIN"));   
       
  3141     
       
  3142     TBool calfilestatus(EFalse);
       
  3143     TBuf8<KBuffLength> keyBuff;
       
  3144     CCalSession* vCalSession = NULL;
       
  3145     CCalSession* vCalSubSession = NULL;   
       
  3146     
       
  3147     vCalSession = CCalSession::NewL();
       
  3148     CleanupStack::PushL(vCalSession);
       
  3149     
       
  3150     CDesCArray* calfilearr = vCalSession->ListCalFilesL();            
       
  3151     CleanupStack::PushL(calfilearr);
       
  3152     
       
  3153     for(TInt i = 0; i < calfilearr->Count(); i++)
       
  3154         {
       
  3155         vCalSubSession = CCalSession::NewL(*vCalSession);
       
  3156         CleanupStack::PushL(vCalSubSession);
       
  3157         vCalSubSession->OpenL(calfilearr->MdcaPoint(i));
       
  3158         
       
  3159         CCalCalendarInfo* caleninfo = vCalSubSession->CalendarInfoL();
       
  3160         CleanupStack::PushL(caleninfo);
       
  3161         
       
  3162         TInt ProfileId;
       
  3163         keyBuff.Zero();
       
  3164         keyBuff.AppendNum( EDeviceSyncProfileID );
       
  3165         TPckgC<TInt> intBuf(ProfileId);
       
  3166         TRAPD(err,intBuf.Set(caleninfo->PropertyValueL(keyBuff)));
       
  3167                     
       
  3168         if( err != KErrNone )
       
  3169             {
       
  3170             FLOG(_L("CNSmlAgendaDataStore::IsCalFileAvailable: Error while retrieving CalFile Property"));
       
  3171             CleanupStack::PopAndDestroy(caleninfo);   
       
  3172             CleanupStack::PopAndDestroy(vCalSubSession);             
       
  3173             continue;
       
  3174             }
       
  3175         
       
  3176         ProfileId = intBuf();
       
  3177         
       
  3178         if(aProfileId == ProfileId)
       
  3179             {
       
  3180             aCalFileArr->AppendL(calfilearr->MdcaPoint(i));
       
  3181             }
       
  3182         CleanupStack::PopAndDestroy(caleninfo);    
       
  3183         CleanupStack::PopAndDestroy(vCalSubSession); 
       
  3184         }
       
  3185     
       
  3186     if( aCalFileArr->Count() > 0 )
       
  3187         {
       
  3188         calfilestatus = ETrue;
       
  3189         }
       
  3190     else
       
  3191         {
       
  3192         calfilestatus = EFalse;
       
  3193         }
       
  3194     CleanupStack::PopAndDestroy(calfilearr);     
       
  3195     CleanupStack::PopAndDestroy(vCalSession);
       
  3196     
       
  3197     FLOG(_L("CNSmlAgendaDataStore::IsCalFileAvailable: END"));    
       
  3198     return calfilestatus;
       
  3199     }
       
  3200 
       
  3201 // -----------------------------------------------------------------------------
       
  3202 // CNSmlAgendaDataStore::OpenStoreL
       
  3203 // Create CalFile with the attributes provided
       
  3204 // -----------------------------------------------------------------------------
       
  3205 HBufC* CNSmlAgendaDataStore::CreateCalFileL( HBufC* aProfileName, TInt aProfileId )
       
  3206     {
       
  3207     FLOG(_L("CNSmlAgendaDataStore::CreateCalFileL: BEGIN"));
       
  3208     
       
  3209     TBuf8<KBuffLength> keyBuff;
       
  3210     TUint calValue = 0;
       
  3211     CCalSession* calSession = CCalSession::NewL();
       
  3212     CleanupStack::PushL(calSession);
       
  3213     CCalCalendarInfo* calinfo = CCalCalendarInfo::NewL();
       
  3214     CleanupStack::PushL(calinfo);   
       
  3215     
       
  3216     //Visibility
       
  3217     calinfo->SetEnabled(ETrue);
       
  3218     calinfo->SetNameL(aProfileName->Des());
       
  3219     // TODO
       
  3220     calinfo->SetColor(Math::Random());
       
  3221     
       
  3222     // Set Meta Data Properties
       
  3223     // LUID Meta Property
       
  3224     keyBuff.Zero();
       
  3225     keyBuff.AppendNum( EFolderLUID );
       
  3226     calValue = CCalenMultiCalUtil::GetNextAvailableOffsetL();
       
  3227     FLOG(_L("CNSmlAgendaDataStore::CreateCalFileL: nextoffset: '%d'"), calValue);
       
  3228     TPckgC<TUint> pckgUidValue( calValue );
       
  3229     calinfo->SetPropertyL( keyBuff, pckgUidValue );
       
  3230     
       
  3231     // Create & Modified Time Meta Property
       
  3232     keyBuff.Zero();
       
  3233     keyBuff.AppendNum( ECreationTime );
       
  3234     TTime time;
       
  3235     time.HomeTime();
       
  3236     TPckgC<TTime> pckgCreateTimeValue( time );
       
  3237     calinfo->SetPropertyL( keyBuff, pckgCreateTimeValue );
       
  3238     keyBuff.Zero();
       
  3239     keyBuff.AppendNum( EModificationTime );
       
  3240     calinfo->SetPropertyL( keyBuff, pckgCreateTimeValue );
       
  3241     
       
  3242     // Sync Status
       
  3243     keyBuff.Zero();
       
  3244     keyBuff.AppendNum( ESyncStatus );
       
  3245     TBool syncstatus( ETrue );
       
  3246     TPckgC<TBool> pckgSyncStatusValue( syncstatus );
       
  3247     calinfo->SetPropertyL( keyBuff, pckgSyncStatusValue );
       
  3248     
       
  3249     // Global UID MetaDataProperty 
       
  3250     keyBuff.Zero();
       
  3251     keyBuff.AppendNum( EGlobalUUID );
       
  3252     HBufC8* guuid = iInterimUtils->GlobalUidL();
       
  3253     TPtr8 guuidPtr = guuid->Des();
       
  3254     CleanupStack::PushL( guuid );
       
  3255     calinfo->SetPropertyL( keyBuff, guuidPtr );
       
  3256     CleanupStack::PopAndDestroy( guuid );
       
  3257     
       
  3258     // Owner
       
  3259     keyBuff.Zero();
       
  3260     TInt syncowner = iAgendaAdapterHandler->DeviceSyncOwner(); 
       
  3261     keyBuff.AppendNum( EDeviceSyncServiceOwner );
       
  3262     TPckgC<TInt> pckgAppUIDValue( syncowner );    
       
  3263     calinfo->SetPropertyL( keyBuff, pckgAppUIDValue );
       
  3264     
       
  3265     // Profile ID Meta Property
       
  3266     keyBuff.Zero();
       
  3267     keyBuff.AppendNum( EDeviceSyncProfileID );
       
  3268     TPckgC<TInt> pckgProfileIdValue( aProfileId );    
       
  3269     calinfo->SetPropertyL( keyBuff, pckgProfileIdValue );
       
  3270        
       
  3271     // Create the CalFile
       
  3272     HBufC* calfilename = CCalenMultiCalUtil::GetNextAvailableCalFileL();
       
  3273     calSession->CreateCalFileL( calfilename->Des(), *calinfo );
       
  3274     CleanupStack::PopAndDestroy(calinfo);
       
  3275     CleanupStack::PopAndDestroy(calSession);
       
  3276     
       
  3277     FLOG(_L("CNSmlAgendaDataStore::CreateCalFileL: END"));
       
  3278     
       
  3279     return calfilename;    
       
  3280     }
       
  3281 
       
  3282 // -----------------------------------------------------------------------------
       
  3283 // CNSmlAgendaDataStore::ActiveItemsL
       
  3284 // Filters out the non-active items from the given array
       
  3285 // -----------------------------------------------------------------------------
       
  3286 CNSmlDataItemUidSet* CNSmlAgendaDataStore::ActiveItemsL( CNSmlDataItemUidSet& aUids ) const
       
  3287     {
       
  3288     FLOG(_L("CNSmlAgendaDataStore::ActiveItemsL: BEGIN"));
       
  3289     
       
  3290     TInt err(KErrNone);
       
  3291     TCalLocalUid uid(0);
       
  3292     TCalLocalUid parentid(0);
       
  3293     CNSmlDataItemUidSet* tempuids = new ( ELeave ) CNSmlDataItemUidSet();
       
  3294     CNSmlDataItemUidSet* inactiveuids = new ( ELeave ) CNSmlDataItemUidSet();
       
  3295     for( TInt count =0; count < aUids.ItemCount(); count++ )
       
  3296         {
       
  3297         parentid = 0;
       
  3298         uid = aUids.ItemAt(count);
       
  3299         TRAP( err, GetCalendarEntryIdL( parentid, uid ));
       
  3300         if ( err && iIsHierarchicalSyncSupported )
       
  3301             {
       
  3302             FLOG(_L("CNSmlAgendaDataStore::ActiveItemsL: Invalid UID"));
       
  3303             delete tempuids;
       
  3304             delete inactiveuids;
       
  3305             User::Leave( KErrGeneral );
       
  3306             }                
       
  3307         if( iAgendaAdapterHandler->FolderSyncStatusL( parentid ) )
       
  3308             {
       
  3309             FLOG(_L("CNSmlAgendaDataStore::ActiveItemsL: ActiveItem: '%d'"), aUids.ItemAt(count));
       
  3310             tempuids->AddItem( aUids.ItemAt(count) );
       
  3311             }
       
  3312         else
       
  3313             {
       
  3314             FLOG(_L("CNSmlAgendaDataStore::ActiveItemsL: InActiveItem: '%d'"), aUids.ItemAt(count));
       
  3315             inactiveuids->AddItem( aUids.ItemAt(count) );
       
  3316             }
       
  3317         }
       
  3318     if( inactiveuids->ItemCount() > 0 )
       
  3319         {
       
  3320         aUids.Reset();
       
  3321         for( TInt count =0; count < tempuids->ItemCount(); count++ )
       
  3322             {
       
  3323             aUids.AddItem( tempuids->ItemAt(count) );
       
  3324             }
       
  3325         delete tempuids;
       
  3326         FLOG(_L("CNSmlAgendaDataStore::ActiveItemsL: END"));
       
  3327         return inactiveuids;
       
  3328         }
       
  3329     else
       
  3330         {
       
  3331         delete tempuids;
       
  3332         delete inactiveuids;
       
  3333         FLOG(_L("CNSmlAgendaDataStore::ActiveItemsL: END"));
       
  3334         return NULL;
       
  3335         }
       
  3336     }
       
  3337 
       
  3338 
       
  3339 // -----------------------------------------------------------------------------
       
  3340 // CNSmlAgendaDataStore::ExternalizeCommittedUidL
       
  3341 // Populate iCommittedUidArr from the Stream
       
  3342 // -----------------------------------------------------------------------------
       
  3343 void CNSmlAgendaDataStore::ExternalizeCommittedUidL() const
       
  3344     {
       
  3345     FLOG(_L("CNSmlAgendaDataStore::ExternalizeCommittedUidL: BEGIN"));
       
  3346     
       
  3347     TUid uid = {KNSmlAgendaAdapterStreamUid};
       
  3348     RWriteStream writeStream;
       
  3349     
       
  3350     // Open stream for writing
       
  3351     iAgendaAdapterLog->iSyncRelationship.OpenWriteStreamLC(writeStream, uid);
       
  3352       
       
  3353     // Write the snapshot to the stream
       
  3354     TInt itemCount = iCommittedUidArr->ItemCount();
       
  3355     FLOG(_L("CNSmlAgendaDataStore::ExternalizeCommittedUidL: count '%d'"), itemCount);
       
  3356     writeStream.WriteInt32L(itemCount);
       
  3357     
       
  3358     if( itemCount > 0 )
       
  3359         {    
       
  3360         for (TInt i = 0; i < itemCount; ++i)
       
  3361             {
       
  3362             writeStream.WriteInt32L(iCommittedUidArr->ItemAt(i));
       
  3363             FLOG(_L("CNSmlAgendaDataStore::ExternalizeCommittedUidL: item '%d'"), iCommittedUidArr->ItemAt(i));
       
  3364             }        
       
  3365         }
       
  3366     writeStream.CommitL();
       
  3367     CleanupStack::PopAndDestroy(); // writeStream
       
  3368     FLOG(_L("CNSmlAgendaDataStore::ExternalizeCommittedUidL: END"));
       
  3369     }
       
  3370 
       
  3371 // -----------------------------------------------------------------------------
       
  3372 // CNSmlAgendaDataStore::InternalizeCommittedUidL
       
  3373 // Write the contents of iCommittedUidArr to the Stream
       
  3374 // -----------------------------------------------------------------------------
       
  3375 void CNSmlAgendaDataStore::InternalizeCommittedUidL() const
       
  3376     {
       
  3377     FLOG(_L("CNSmlAgendaDataStore::InternalizeCommittedUidL: BEGIN"));
       
  3378   
       
  3379     TUid uid = {KNSmlAgendaAdapterStreamUid};        
       
  3380     TBool hashistory = iAgendaAdapterLog->iSyncRelationship.IsStreamPresentL(uid);
       
  3381     
       
  3382     if (hashistory)
       
  3383         {
       
  3384         FLOG(_L("CNSmlAgendaDataStore::InternalizeCommittedUidL: HasHistory"));
       
  3385         
       
  3386         // Open stream for reading
       
  3387         RReadStream readStream;
       
  3388         iAgendaAdapterLog->iSyncRelationship.OpenReadStreamLC(readStream, uid);
       
  3389         
       
  3390         // Read snapshot from the stream
       
  3391         TSmlDbItemUid item;
       
  3392         TInt itemCount(readStream.ReadInt32L());
       
  3393         FLOG(_L("CNSmlAgendaDataStore::InternalizeCommittedUidL: count '%d'"), itemCount);
       
  3394         iCommittedUidArr->Reset();
       
  3395         for (TInt i = 0; i < itemCount; ++i)
       
  3396             {
       
  3397             item = readStream.ReadInt32L();
       
  3398             FLOG(_L("CNSmlAgendaDataStore::InternalizeCommittedUidL: item '%d'"), item);
       
  3399             iCommittedUidArr->AddItem(item);
       
  3400             }        
       
  3401         CleanupStack::PopAndDestroy(); // readStream
       
  3402         }
       
  3403 
       
  3404     FLOG(_L("CNSmlAgendaDataStore::InternalizeCommittedUidL: END"));
       
  3405     }
       
  3406 
       
  3407 // -----------------------------------------------------------------------------
       
  3408 // CNSmlAgendaDataStoreUtil::CNSmlAgendaDataStoreUtil
       
  3409 // C++ default constructor can NOT contain any code, that
       
  3410 // might leave.
       
  3411 // -----------------------------------------------------------------------------
       
  3412 //
       
  3413 CNSmlAgendaDataStoreUtil::CNSmlAgendaDataStoreUtil() :
       
  3414     iCalSession( NULL ),
       
  3415     iExporter( NULL ),
       
  3416     iImporter( NULL ),
       
  3417     iProgressView( NULL ),
       
  3418     iEntryView( NULL ),
       
  3419     iEntry( NULL ),
       
  3420     iFileName( NULL )
       
  3421     {
       
  3422     FLOG(_L("CNSmlAgendaDataStoreUtil::CNSmlAgendaDataStoreUtil(): BEGIN"));
       
  3423     
       
  3424     FLOG(_L("CNSmlAgendaDataStoreUtil::CNSmlAgendaDataStoreUtil(): END"));
       
  3425     }
       
  3426 
       
  3427 // -----------------------------------------------------------------------------
       
  3428 // CNSmlAgendaDataStoreUtil::InitializeCalAPIsL
       
  3429 // Initialize Calendar APIs for database access using the provided name and id
       
  3430 // -----------------------------------------------------------------------------
       
  3431 //
       
  3432 void CNSmlAgendaDataStoreUtil::InitializeCalAPIsL( HBufC* aFileName, TSmlDbItemUid aUid )
       
  3433     {
       
  3434     FLOG(_L("CNSmlAgendaDataStoreUtil::InitializeCalAPIsL: BEGIN"));
       
  3435     
       
  3436     iCalSession = CCalSession::NewL();
       
  3437     iCalSession->OpenL(aFileName->Des());        
       
  3438     iExporter = CCalenExporter::NewL( *iCalSession );
       
  3439     iImporter = CCalenImporter::NewL( *iCalSession );
       
  3440     iProgressView = CNSmlAgendaProgressview::NewL();
       
  3441     iEntryView = CCalEntryView::NewL( *iCalSession, *iProgressView );
       
  3442     CActiveScheduler::Start();
       
  3443     TInt completedStatus = iProgressView->GetCompletedStatus();
       
  3444     if ( completedStatus != KErrNone )
       
  3445         {
       
  3446         FLOG(_L("CNSmlAgendaDataStoreUtil::ConstructL: ERROR in Progressview"));
       
  3447         }
       
  3448     if( aUid != NULL )
       
  3449         {
       
  3450         iEntry = iEntryView->FetchL( aUid );
       
  3451         if( NULL == iEntry )
       
  3452             {
       
  3453             User::Leave( KErrNotFound );
       
  3454             }
       
  3455         
       
  3456         }    
       
  3457     FLOG(_L("CNSmlAgendaDataStoreUtil::InitializeCalAPIsL: END"));
       
  3458     }
       
  3459 
       
  3460 // -----------------------------------------------------------------------------
       
  3461 // CNSmlAgendaDataStoreUtil::NewL
       
  3462 // Two-phased constructor.
       
  3463 // -----------------------------------------------------------------------------
       
  3464 //
       
  3465 CNSmlAgendaDataStoreUtil* CNSmlAgendaDataStoreUtil::NewL()
       
  3466     {
       
  3467     FLOG(_L("CNSmlAgendaDataStoreUtil::NewL: BEGIN"));
       
  3468     
       
  3469     CNSmlAgendaDataStoreUtil* self = new ( ELeave ) CNSmlAgendaDataStoreUtil();
       
  3470     
       
  3471     FLOG(_L("CNSmlAgendaDataStoreUtil::NewL: END"));
       
  3472     return self;
       
  3473     }
       
  3474 
       
  3475 // -----------------------------------------------------------------------------
       
  3476 // CNSmlAgendaDataStoreUtil::~CNSmlAgendaDataStoreUtil
       
  3477 // Destructor.
       
  3478 // -----------------------------------------------------------------------------
       
  3479 //
       
  3480 CNSmlAgendaDataStoreUtil::~CNSmlAgendaDataStoreUtil()
       
  3481     {
       
  3482     FLOG(_L("CNSmlAgendaDataStoreUtil::~CNSmlAgendaDataStoreUtil(): BEGIN"));
       
  3483 
       
  3484     delete iEntry;
       
  3485     delete iEntryView;
       
  3486     delete iExporter;
       
  3487     delete iImporter;
       
  3488     delete iCalSession;
       
  3489     delete iProgressView;
       
  3490     
       
  3491     FLOG(_L("CNSmlAgendaDataStoreUtil::~CNSmlAgendaDataStoreUtil(): END"));
       
  3492     }
       
  3493 
       
  3494 // -----------------------------------------------------------------------------
       
  3495 // CNSmlAgendaAdapterLog::CNSmlAgendaAdapterLog
       
  3496 // C++ default constructor can NOT contain any code, that
       
  3497 // might leave.
       
  3498 // -----------------------------------------------------------------------------
       
  3499 //
       
  3500 CNSmlAgendaAdapterLog::CNSmlAgendaAdapterLog( MSmlSyncRelationship& aSyncRelationship ) :
       
  3501     iSyncRelationship( aSyncRelationship )
       
  3502     {
       
  3503     FLOG(_L("CNSmlAgendaAdapterLog::CNSmlAgendaAdapterLog(): BEGIN"));
       
  3504     
       
  3505     FLOG(_L("CNSmlAgendaAdapterLog::CNSmlAgendaAdapterLog(): END"));
       
  3506     }
       
  3507 
       
  3508 // -----------------------------------------------------------------------------
       
  3509 // CNSmlAgendaAdapterLog::NewL
       
  3510 // Two-phased constructor.
       
  3511 // -----------------------------------------------------------------------------
       
  3512 //
       
  3513 CNSmlAgendaAdapterLog* CNSmlAgendaAdapterLog::NewL( MSmlSyncRelationship& aSyncRelationship )
       
  3514     {
       
  3515     FLOG(_L("CNSmlAgendaAdapterLog::NewL: BEGIN"));
       
  3516     
       
  3517     CNSmlAgendaAdapterLog* self = new ( ELeave ) CNSmlAgendaAdapterLog(aSyncRelationship);
       
  3518     
       
  3519     FLOG(_L("CNSmlAgendaAdapterLog::NewL: END"));
       
  3520     return self;
       
  3521     }
       
  3522 
       
  3523 // -----------------------------------------------------------------------------
       
  3524 // CNSmlAgendaAdapterLog::~CNSmlAgendaAdapterLog
       
  3525 // Destructor.
       
  3526 // -----------------------------------------------------------------------------
       
  3527 //
       
  3528 CNSmlAgendaAdapterLog::~CNSmlAgendaAdapterLog()
       
  3529     {
       
  3530     FLOG(_L("CNSmlAgendaAdapterLog::~CNSmlAgendaAdapterLog(): BEGIN"));
       
  3531     
       
  3532     FLOG(_L("CNSmlAgendaAdapterLog::~CNSmlAgendaAdapterLog(): END"));
       
  3533     }
       
  3534 
       
  3535    
       
  3536 //  End of File