pimappservices/calendar/client/src/calasynctask.cpp
changeset 0 f979ecb2b13e
child 60 96907930389d
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "agmsimpleentry.h"
       
    17 #include "calclient.h"
       
    18 #include "calcategoryimpl.h"
       
    19 #include "calentryimpl.h"
       
    20 #include <calprogresscallback.h>
       
    21 #include "calsessionimpl.h"
       
    22 #include "calcategorymanagerimpl.h"
       
    23 #include "agmdebug.h"
       
    24 
       
    25 CCalAsyncTaskManager::CCalAsyncTaskManager(CCalSessionImpl& aSessionImpl)
       
    26 	: CActive(EPriorityStandard),
       
    27 	iSessionImpl(aSessionImpl)
       
    28 	{
       
    29 	CActiveScheduler::Add(this);
       
    30 	}
       
    31 	
       
    32 CCalAsyncTaskManager::~CCalAsyncTaskManager()
       
    33 	{
       
    34 	CancelAsynTask();
       
    35 	iCallBacks.Close();
       
    36 	delete iEntryIdList;
       
    37 	}
       
    38 	
       
    39 CCalAsyncTaskManager* CCalAsyncTaskManager::NewL(CCalSessionImpl& aSessionImpl)
       
    40 	{
       
    41 	CCalAsyncTaskManager* self = new (ELeave) CCalAsyncTaskManager(aSessionImpl);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 void CCalAsyncTaskManager::StartBuildIndexL(MCalProgressCallBack& aProgressCallBack)
       
    46 	{
       
    47 	/*If it is a task of building index, place the callback into the queue, 
       
    48 	so that all of them can be completed when the building index. 
       
    49 	*/
       
    50 	_DBGLOG_ASYNC(AgmDebug::DebugLogTimeStampL("StartBuildIndexL: Commencing async build index");)
       
    51 	
       
    52 	if (iAction==EBuildIndex)
       
    53 		{
       
    54 		iCallBacks.AppendL(&aProgressCallBack);
       
    55 		}
       
    56 	else if (iAction!=ENoAction)
       
    57 		{
       
    58 		//The index must have been built if it is doing other asynchronous operations
       
    59 		aProgressCallBack.Completed(KErrNone);
       
    60 		}
       
    61 	else
       
    62 		{
       
    63 		iAction = EBuildIndex;
       
    64 		iCallBacks.AppendL(&aProgressCallBack);
       
    65 
       
    66 		if (!IsActive())
       
    67 			{
       
    68 			SetActive();
       
    69 			}
       
    70 		iSessionImpl.Server().StartBuildIndex(aProgressCallBack.NotifyProgress(), iStatus, iSessionImpl.CollectionId());
       
    71 		}
       
    72 	}
       
    73 
       
    74 void CCalAsyncTaskManager::StartDeleteEntryL(MCalProgressCallBack& aProgressCallBack, const CalCommon::TCalTimeRange& aCalTimeRange, CalCommon::TCalViewFilter aFilter)
       
    75 	{
       
    76 	/*If it is a task of building index, place the callback into the queue, 
       
    77 	so that all of them can be completed when the building index. If it is a
       
    78 	task of deleting entry, simply leave with "busy". 
       
    79 	*/
       
    80 	_DBGLOG_ASYNC(AgmDebug::DebugLogTimeStampL("StartDeleteEntryL: Commencing async delete");)
       
    81 	
       
    82 	if(iAction!=ENoAction)
       
    83 		{
       
    84 		User::Leave(KErrLocked);
       
    85 		}
       
    86 		
       
    87 	TTime todaysDate;
       
    88 	todaysDate.HomeTime();
       
    89 
       
    90 	iAction = EDeleteEntry;
       
    91 	iCallBacks.Append(&aProgressCallBack);
       
    92 
       
    93 	TAgnFilter agnFilter(aFilter, CalCommon::EFoldedTextSearch);
       
    94 	TRAPD(err, iSessionImpl.Server().StartTidyByDateL(aProgressCallBack.NotifyProgress(), iStatus, agnFilter, todaysDate, aCalTimeRange, iSessionImpl.CollectionId()));
       
    95 
       
    96 	if (err != KErrNone)
       
    97 		{
       
    98 		iCallBacks.Remove(iCallBacks.Count() - 1);
       
    99 		iAction = ENoAction;
       
   100 		User::Leave(err);
       
   101 		}
       
   102 	else if (!IsActive())
       
   103 		{
       
   104 		SetActive();
       
   105 		}
       
   106 	}
       
   107 	
       
   108 void CCalAsyncTaskManager::StartCategoryTaskL(MCalProgressCallBack& aProgressCallBack, TAsyncAction aAction, const CCalCategory& aCategory, RPointerArray<CCalEntry>* aFilteredEntry)
       
   109 	{
       
   110 	/*If it is a task of building index, place the callback into the queue, 
       
   111 	so that all of them can be completed when the building index. If it is a
       
   112 	task of deleting entry, simply leave with "busy". 
       
   113 	*/
       
   114 	_DBGLOG_ASYNC(AgmDebug::DebugLogTimeStampL("StartCategoryTaskL: Commencing async category task");)
       
   115 	
       
   116 	if(iAction!=ENoAction)
       
   117 		{
       
   118 		User::Leave(KErrLocked);
       
   119 		}
       
   120 		
       
   121 
       
   122 	iAction = aAction;
       
   123 	iCallBacks.Append(&aProgressCallBack);
       
   124 
       
   125 	if(iAction==EFilterCategory)
       
   126 		{
       
   127 		iFilteredEntry = aFilteredEntry;
       
   128 		}
       
   129 
       
   130 	TRAPD(err, iSessionImpl.Server().StartCategoryTaskL(aProgressCallBack.NotifyProgress(), iStatus, *(aCategory.Impl()->AgnCategory()), aAction, iSessionImpl.CollectionId()));
       
   131 	if (err != KErrNone)
       
   132 		{
       
   133 		iCallBacks.Remove(iCallBacks.Count() - 1);
       
   134 		iAction = ENoAction;
       
   135 		User::Leave(err);
       
   136 		}
       
   137 	else if (!IsActive())
       
   138 		{
       
   139 		SetActive();
       
   140 		}
       
   141 	}
       
   142 
       
   143 		
       
   144 void CCalAsyncTaskManager::DoCancel()
       
   145 	{
       
   146 	}
       
   147 
       
   148 void CCalAsyncTaskManager::CancelAsynTask()
       
   149 	{
       
   150 	if (iAction != ENoAction)
       
   151 		{
       
   152 		iSessionImpl.Server().CancelTask(iSessionImpl.FileId());
       
   153 		CompleteRequest(KErrCancel);
       
   154 		Cancel();
       
   155 		}
       
   156 	iAction = ENoAction;
       
   157 	}
       
   158 
       
   159 void CCalAsyncTaskManager::RunL()
       
   160 	{
       
   161 	//If the progress value is 100 or negative it means that the observed process
       
   162 	//has finished and thereby it should have provided the progress value to report
       
   163 	//Under normal conditions we use the complete status as a progress parameter (%)
       
   164 	TInt progress = iStatus.Int();
       
   165 
       
   166 	//If The ongoing process is not cancelled or finished then request again
       
   167 	if (progress > 0 && progress < 100)
       
   168 		{
       
   169 		TInt count=iCallBacks.Count();
       
   170 		for(TInt ii=0; ii<count; ++ii)
       
   171 			{
       
   172 			if((iCallBacks)[ii])
       
   173 				{
       
   174 				(iCallBacks)[ii]->Progress(progress);
       
   175 				}
       
   176 			}
       
   177 
       
   178 		//requesting itself
       
   179 		if (iAction == EBuildIndex)
       
   180 			{//When asynchronous operation is to build index, we should call RAgendaServ::StartBuildIndex  for progress requess. 
       
   181 			iSessionImpl.Server().StartBuildIndex(ETrue, iStatus, iSessionImpl.CollectionId());
       
   182 			}
       
   183 		else
       
   184 			{
       
   185 			iSessionImpl.Server().RequestProgress(iStatus, iSessionImpl.CollectionId());
       
   186 			}
       
   187 		
       
   188 		SetActive();
       
   189 		}
       
   190 	else 
       
   191 		{
       
   192 		if (iAction==EFilterCategory && progress==KErrNone)
       
   193 			{
       
   194 			TRAP(progress,GetFilteredEntryL());
       
   195 			}
       
   196 		CompleteRequest(progress);
       
   197 		}
       
   198 	}
       
   199 
       
   200 void CCalAsyncTaskManager::CompleteRequest(TInt aCompleteCode)
       
   201 	{
       
   202 	for(TInt ii=0; ii<iCallBacks.Count(); ++ii)
       
   203 		{
       
   204 		if((iCallBacks)[ii])
       
   205 			{
       
   206 			iCallBacks[ii]->Completed(aCompleteCode);
       
   207 			}
       
   208 		}
       
   209 	iCallBacks.Reset();
       
   210 	delete iEntryIdList;
       
   211 	iEntryIdList=NULL;
       
   212 	iAction = ENoAction;
       
   213 	}
       
   214 	
       
   215 void CCalAsyncTaskManager::GetFilteredEntryL()
       
   216 	{// Get the light entry by its entry id and fill in it into the array past in by the client
       
   217 	iEntryIdList = new (ELeave) CArrayFixSeg<TAgnEntryId> (32);
       
   218 	CCalCategoryManagerImpl::GetFilteredEntryL(*iEntryIdList, *iFilteredEntry, iSessionImpl);
       
   219 	delete iEntryIdList;
       
   220 	iEntryIdList=NULL;
       
   221 	}
       
   222 
       
   223 TBool CCalAsyncTaskManager::BuildingIndex() const
       
   224 	{
       
   225 	return (iAction == EBuildIndex);
       
   226 	}
       
   227 	
       
   228 TBool CCalAsyncTaskManager::IsBusy() const
       
   229 	{
       
   230 	return iAction!=ENoAction;	
       
   231 	}