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