sapi_calendar/calendarservice/src/calendardeleteentry.cpp
changeset 0 14df0fbfcc4e
equal deleted inserted replaced
-1:000000000000 0:14df0fbfcc4e
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <caliterator.h>
       
    20 #include <calentryview.h>
       
    21 #include <calinstanceview.h>
       
    22 #include <calinstance.h>
       
    23 
       
    24 #include "calendarheader.h"
       
    25 #include "calendarconstants.h"
       
    26 #include "asyncreqobserver.h"
       
    27 #include "calendardeleteentry.h"
       
    28 
       
    29 void CleanupCCalEntryArray(TAny* aPointer)
       
    30 	{
       
    31 	RPointerArray<CCalEntry>* calEntryArray = static_cast<RPointerArray<CCalEntry>*>(aPointer); 
       
    32 	if ( calEntryArray )
       
    33 		{
       
    34 		calEntryArray->ResetAndDestroy();
       
    35 		}
       
    36 	}
       
    37 
       
    38 void CleanupCCalInstanceArray(TAny* aPointer)
       
    39 	{
       
    40 	RPointerArray<CCalInstance>* calInstanceArray = static_cast<RPointerArray<CCalInstance>*>(aPointer); 
       
    41 	if ( calInstanceArray )
       
    42 		{
       
    43 		calInstanceArray->ResetAndDestroy();
       
    44 		}
       
    45 	}
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // Two-phased constructor.
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CCalendarDeleteEntry* CCalendarDeleteEntry::NewL( CCalendarSessionInfo* aSessionInfo, 
       
    52 													CCalendarFilter* aFilter,
       
    53 													CAsyncRequestObserver* aAsyncRequestObserver,
       
    54 													MCalCallbackBase* aNotifyCallback )
       
    55 	{
       
    56 	CCalendarDeleteEntry* self = new (ELeave) CCalendarDeleteEntry( aSessionInfo, aAsyncRequestObserver, aNotifyCallback );
       
    57 	CleanupStack::PushL( self );
       
    58 	// Construct filter object which determines the type of delete 
       
    59 	self->ConstructL( aFilter );
       
    60 	CleanupStack::Pop( self );
       
    61 	return self;
       
    62 	}
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // Destructor.
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CCalendarDeleteEntry::~CCalendarDeleteEntry()
       
    69 	{
       
    70 	Cancel();
       
    71 	
       
    72 	delete iFilter;
       
    73 	}
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Constructor.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CCalendarDeleteEntry::CCalendarDeleteEntry( CCalendarSessionInfo* aSessionInfo, 
       
    80 												CAsyncRequestObserver* aAsyncRequestObserver,
       
    81 												MCalCallbackBase* aNotifyCallback  ):
       
    82 					//CActive( EPriorityStandard ),
       
    83 					iSessionInfo( aSessionInfo ),
       
    84 					iNotifyCallback ( aNotifyCallback ),
       
    85 					iAsyncRequestObserver( aAsyncRequestObserver )
       
    86 	{
       
    87 	}
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // ConstructL
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 void CCalendarDeleteEntry::ConstructL( CCalendarFilter* aFilter )
       
    94 	{
       
    95 	iFilter = CCalendarFilter::NewL();
       
    96 	*iFilter = *aFilter;
       
    97 	}
       
    98 	
       
    99 // ---------------------------------------------------------------------------
       
   100 // Delete Request
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CCalendarDeleteEntry::DeleteL()
       
   104 	{
       
   105 	if ( iNotifyCallback ) // making call as asynchronous
       
   106 		{
       
   107 		if( !iAsyncRequestObserver )
       
   108 			User::Leave( KErrArgument );
       
   109 		
       
   110 		CActiveScheduler::Add( this );
       
   111 		ActivateRequest( KErrNone );
       
   112 		}
       
   113 	else				// making call as synchronous
       
   114 		{ 
       
   115 		// Sets the parameters for the delete operation
       
   116 		InitializeDeleteOptionL();
       
   117 		// Performs the actual delete depending on the parameters set
       
   118 		StartDeleteL();
       
   119 		}
       
   120 	}
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Inherited from CActive class 
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CCalendarDeleteEntry::DoCancel()
       
   127 	{
       
   128 	NotifyRequestResult( KErrCancel );
       
   129 	}
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // Inherited from CActive class 
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 void CCalendarDeleteEntry::RunL()
       
   136 	{
       
   137 	TInt err = iStatus.Int();
       
   138 	if ( err == KErrNone )
       
   139 		{
       
   140 		// Sets the parameters for the delete operation
       
   141 		InitializeDeleteOptionL();
       
   142         // Performs the actual delete depending on the parameters set
       
   143 		TRAP(err, StartDeleteL());
       
   144 		}
       
   145 
       
   146 	NotifyRequestResult( err );		
       
   147 	}
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // Delete All entries from the calendar
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 void CCalendarDeleteEntry::DeleteAllL()
       
   154 	{
       
   155 	// Create an instance of CCalIter to iterate through all calendar entries
       
   156 	CCalIter* calIter = CCalIter::NewL(*(iSessionInfo->Session()));
       
   157 	
       
   158 	CleanupStack::PushL( calIter );
       
   159 	
       
   160 	CDesC8Array* array = new(ELeave) CDesC8ArraySeg( KArrayGran ); 
       
   161 
       
   162 	CleanupStack::PushL( array );
       
   163 	
       
   164 	TPtrC8 firstUid( calIter->FirstL() );
       
   165 	
       
   166 	while ( firstUid != KNullDesC8 )
       
   167 		{
       
   168 		array->AppendL( firstUid );
       
   169 		firstUid.Set( calIter->NextL() );
       
   170 		}
       
   171     // Delete all calendar entries using entry view object
       
   172 	iSessionInfo->EntryView()->DeleteL( *array );
       
   173 	
       
   174 	array->Reset();
       
   175 	
       
   176 	CleanupStack::PopAndDestroy(2, calIter);
       
   177 	}
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // Delete Instances based on Time Range
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CCalendarDeleteEntry::DeleteTimeRangeL()
       
   184 	{
       
   185 	RPointerArray<CCalInstance> array;
       
   186 
       
   187 	CleanupStack::PushL( TCleanupItem(CleanupCCalInstanceArray, &array) );
       
   188 	
       
   189 	CalCommon::TCalTimeRange range =  iFilter->TimeRange();
       
   190 
       
   191 	if( range.EndTime().TimeLocalL() != Time::NullTTime() && 
       
   192 		range.StartTime().TimeLocalL() != Time::NullTTime() )
       
   193 		{
       
   194 		if( range.EndTime().TimeLocalL() < range.StartTime().TimeLocalL() )
       
   195 			User::Leave( KErrArgument );
       
   196 		}
       
   197 	
       
   198 	
       
   199 	// Find all instances in specfied time range using instance view object
       
   200 	iSessionInfo->InstanceView()->FindInstanceL( array, iFilter->FilterType(), 
       
   201 														iFilter->TimeRange() );
       
   202 	
       
   203 	TInt count = array.Count();
       
   204 	
       
   205 	if ( count > 0 )
       
   206 		{
       
   207 		for ( TInt index = 0; index < count; index++ )
       
   208 			{
       
   209 			// Delete instances one by one using instance view object
       
   210 			iSessionInfo->InstanceView()->DeleteL( array[index], CalCommon::EThisOnly );
       
   211 			array[index] = NULL;
       
   212 			}
       
   213 		}
       
   214 		
       
   215 	CleanupStack::PopAndDestroy( &array );	
       
   216 	}
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // Delete Entries based in GlobalUid/LocalUid
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 void CCalendarDeleteEntry::DeleteUidL( TBool aLocalUid )
       
   223 	{
       
   224 	if ( aLocalUid )
       
   225 		{
       
   226 		TInt num;
       
   227 		// Delete all entries with local uids as specified
       
   228 		iSessionInfo->EntryView()->DeleteL( iFilter->LocalUidList(), num );
       
   229 		}
       
   230 	else
       
   231 		{
       
   232 		TInt count = iFilter->GuidList()->Count();
       
   233 		for(TInt index = 0; index < count; index++)
       
   234 			{
       
   235 			RPointerArray<CCalEntry> calEntryArray;
       
   236 			iSessionInfo->EntryView()->FetchL( iFilter->GuidList()->MdcaPoint(index), calEntryArray );
       
   237 			if ( calEntryArray.Count() )
       
   238 				{
       
   239 	            CleanupStack::PushL( TCleanupItem(CleanupCCalEntryArray, &calEntryArray) );
       
   240 				iSessionInfo->EntryView()->DeleteL( *calEntryArray[0] );
       
   241 				CleanupStack::Pop( &calEntryArray );
       
   242 				}
       
   243 			
       
   244 			calEntryArray.ResetAndDestroy();	
       
   245 			// Delete all entries with global uids as specified
       
   246 			//iSessionInfo->EntryView()->DeleteL( *iFilter->GuidList() );
       
   247 			}
       
   248 		// Delete all entries with global uids as specified
       
   249 		//iSessionInfo->EntryView()->DeleteL( *iFilter->GuidList() );
       
   250 		}	
       
   251 	}
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // Delete Instances based on Time Range and Global Uid
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void CCalendarDeleteEntry::DeleteTimeRangeWithGlobalUidL()
       
   258 	{
       
   259 	RPointerArray<CCalInstance> array;
       
   260 	
       
   261 	CleanupStack::PushL( TCleanupItem(CleanupCCalInstanceArray, &array) );
       
   262 	
       
   263 	CalCommon::TCalTimeRange range =  iFilter->TimeRange();
       
   264 
       
   265 	if( range.EndTime().TimeLocalL() != Time::NullTTime() && 
       
   266 		range.StartTime().TimeLocalL() != Time::NullTTime() )
       
   267 		{
       
   268 		if( range.EndTime().TimeLocalL() < range.StartTime().TimeLocalL() )
       
   269 			User::Leave( KErrArgument );
       
   270 		}
       
   271 
       
   272 	// Find all instances matching filter criteria
       
   273 	iSessionInfo->InstanceView()->FindInstanceL( array, iFilter->FilterType(), iFilter->TimeRange() );
       
   274 	
       
   275 	for ( TInt index = 0; index < array.Count(); index++ )
       
   276 		{
       
   277 		TInt pos = 0;
       
   278 		if( iFilter->GuidList()->Find( array[index]->Entry().UidL(), pos ) == 0 )
       
   279 			{
       
   280 			// Delete only those instances matching specified set of global uids
       
   281 			iSessionInfo->InstanceView()->DeleteL( array[index], CalCommon::EThisOnly );
       
   282 			array[index] = NULL;
       
   283 			}
       
   284 		}
       
   285 	
       
   286 	CleanupStack::PopAndDestroy( &array );	
       
   287 	}
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // Delete Instances based on Time Range and Local Uid
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 void CCalendarDeleteEntry::DeleteTimeRangeWithLocalUidL()
       
   294 	{
       
   295 	RPointerArray<CCalInstance> array;
       
   296 	
       
   297 	CleanupStack::PushL( TCleanupItem(CleanupCCalInstanceArray, &array) );
       
   298 	
       
   299 	const RArray<TCalLocalUid> &calLocalUidArray = iFilter->LocalUidList();
       
   300 	
       
   301 	CalCommon::TCalTimeRange range =  iFilter->TimeRange();
       
   302 
       
   303 	if( range.EndTime().TimeLocalL() != Time::NullTTime() && 
       
   304 		range.StartTime().TimeLocalL() != Time::NullTTime() )
       
   305 		{
       
   306 		if( range.EndTime().TimeLocalL() < range.StartTime().TimeLocalL() )
       
   307 			User::Leave( KErrArgument );
       
   308 		}
       
   309 
       
   310     // Find all instances matching filter criteria
       
   311 	iSessionInfo->InstanceView()->FindInstanceL( array, iFilter->FilterType(), iFilter->TimeRange() );
       
   312 	
       
   313 	TInt count = array.Count();
       
   314 	
       
   315 	if ( count > 0 )
       
   316 		{
       
   317 		for ( TInt index = 0; index < count; index++ )
       
   318 			{
       
   319 			if( calLocalUidArray.Find( array[index]->Entry().LocalUidL() ) != KErrNotFound )
       
   320 				{
       
   321 				//// Delete only those instances matching specified set of local uids
       
   322 				iSessionInfo->InstanceView()->DeleteL( array[index], CalCommon::EThisOnly );
       
   323 				array[index] = NULL;
       
   324 				}
       
   325 			}
       
   326 		}
       
   327 		
       
   328 	CleanupStack::PopAndDestroy( &array );
       
   329 	}
       
   330 	
       
   331 // ---------------------------------------------------------------------------
       
   332 // Call the appropriate delete based in delete option
       
   333 // ---------------------------------------------------------------------------
       
   334 //
       
   335 void CCalendarDeleteEntry::StartDeleteL()
       
   336 	{
       
   337 	switch ( iDeleteOption )
       
   338 		{
       
   339 		case EDeleteAll:// delete all entries
       
   340 			DeleteAllL();
       
   341 			break;
       
   342 
       
   343 		case EDeleteGUid: // delete on guid - entries
       
   344 			DeleteUidL( EFalse );
       
   345 			break;
       
   346 			
       
   347 		case EDeleteLUid: // delete on luid - etries
       
   348 			DeleteUidL( ETrue );
       
   349 			break;
       
   350 			
       
   351 		case EDeleteTimeRange: // delete on time range - instances
       
   352 			DeleteTimeRangeL();
       
   353 			break;
       
   354 			
       
   355 		case EDeleteTmRngGUid: // delete on time range with global uid - instances
       
   356 			DeleteTimeRangeWithGlobalUidL();
       
   357 			break;
       
   358 			
       
   359 		case EDeleteTmRngLUid: // delete on time range with local uid - instances
       
   360 			DeleteTimeRangeWithLocalUidL();
       
   361 			break;
       
   362 		
       
   363 		default:
       
   364 			break;
       
   365 		}
       
   366 	}
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // Initialises delete option
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 void CCalendarDeleteEntry::InitializeDeleteOptionL()
       
   373 	{
       
   374 	//Delete all entries from calendar
       
   375 	if ( iFilter->Filter() == EFlagDeleteAll )
       
   376 		{
       
   377 		iDeleteOption = EDeleteAll;
       
   378 		}
       
   379 	
       
   380 	//Filter having only GUids
       
   381 	else if ( iFilter->Filter() == EFilterGUid )
       
   382 		{
       
   383 		iDeleteOption = EDeleteGUid;
       
   384 		}
       
   385 		
       
   386 
       
   387 	//Filter having only LocalUids
       
   388 	else if ( iFilter->Filter() == EFilterLUid )
       
   389 		{
       
   390 		iDeleteOption = EDeleteLUid;
       
   391 		}
       
   392 	
       
   393 	
       
   394 	//Filter having time range only (No GUid/LocalUid)
       
   395 	else if ( !( iFilter->Filter() & EFilterGUid ) &&
       
   396 				!( iFilter->Filter() & EFilterLUid ) )
       
   397 		{
       
   398 		if ( ( iFilter->Filter() & EFilterStTime ) || ( iFilter->Filter() & EFilterEndTime ) )
       
   399 			iDeleteOption = EDeleteTimeRange;
       
   400 		else
       
   401 			iDeleteOption = EDeleteAll;
       
   402 		}
       
   403 
       
   404 	//Filter having time range and GUid
       
   405 	else if ( ( iFilter->Filter() & EFilterGUid ) && 
       
   406 				( ( iFilter->Filter() & EFilterStTime ) || ( iFilter->Filter() & EFilterEndTime ) ) )// bound time only
       
   407 		{
       
   408 		iDeleteOption = EDeleteTmRngGUid;
       
   409 		}
       
   410 
       
   411 	//Filter having time range and LocalUid
       
   412 	else if ( ( iFilter->Filter() & EFilterLUid ) && 
       
   413 				( ( iFilter->Filter() & EFilterStTime ) || ( iFilter->Filter() & EFilterEndTime ) ) )
       
   414 		{
       
   415 		iDeleteOption = EDeleteTmRngLUid;
       
   416 		}
       
   417 	else
       
   418 		{
       
   419 		User::Leave( KErrArgument );
       
   420 		}
       
   421 	}
       
   422 
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // Activates the asynchronous request
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 void CCalendarDeleteEntry::ActivateRequest( TInt aReason )
       
   429 	{
       
   430 	iStatus = KRequestPending;
       
   431 	SetActive();
       
   432 	TRequestStatus* temp = &iStatus;
       
   433 	User::RequestComplete( temp, aReason );
       
   434 	}
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // Notifies callback the result for asynchronous request.
       
   438 // ---------------------------------------------------------------------------
       
   439 //
       
   440 void CCalendarDeleteEntry::NotifyRequestResult( TInt aReason )
       
   441 	{
       
   442 	if ( iNotifyCallback )
       
   443 		{
       
   444 		iAsyncRequestObserver->RequestComplete( iNotifyCallback->iTransactionId );
       
   445 		TRAPD( err, iNotifyCallback->NotifyResultL( aReason, NULL ));
       
   446 		}
       
   447 	
       
   448 	// caller will delete the object in case of cancel
       
   449 	if ( aReason != KErrCancel )
       
   450 		delete this;
       
   451 	}
       
   452