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