Update contrib.
1 // Copyright (c) 2004-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include "SchLogger.h"
25 const TInt KMaxTasksPerSchedule = 9999;
28 TScheduledTask::TScheduledTask(CScheduledTask& aTask, CClientProxy& aClient)
34 void TScheduledTask::OnDue(const TTsTime& aValidUntil)
36 LOGSTRING("TScheduledTask::OnDue - start");
37 iTask.OnDue(aValidUntil);
38 iClient.ReadyToExecute();
39 LOGSTRING("TScheduledTask::OnDue - end");
42 const HBufC& TScheduledTask::Data() const
47 const TTaskInfo& TScheduledTask::Info() const
52 const CClientProxy& TScheduledTask::Client() const
57 void TScheduledTask::RemoveInfo()
59 LOGSTRING("TScheduledTask::RemoveInfo - start");
60 iClient.RemoveTask(&iTask);
61 LOGSTRING("TScheduledTask::RemoveInfo - end");
64 void TScheduledTask::DecRepeat()
69 TInt TScheduledTask::Offset()
71 return (_FOFF(TScheduledTask, iLink));
73 /*************************************************************************/
75 /*************************************************************************/
76 CSchedule* CSchedule::NewLC(TInt aHandle,
79 const CArrayFixFlat<TScheduleEntryInfo2>& aEntries,
80 const TSecurityInfo& aSecurityInfo)
82 CSchedule* self = new(ELeave) CSchedule(aSecurityInfo, aHandle, aPersists);
83 CleanupStack::PushL(self);
84 self->ConstructL(aName, aEntries);
88 CSchedule* CSchedule::NewLC(TInt aHandle,
91 const CArrayFixFlat<TTaskSchedulerCondition>& aEntries,
92 const TTsTime& aDefaultRunTime,
93 const TSecurityInfo& aSecurityInfo)
95 CSchedule* self = new(ELeave) CSchedule(aSecurityInfo, aHandle, aPersists);
96 CleanupStack::PushL(self);
97 self->ConstructL(aName, aEntries,aDefaultRunTime);
101 CSchedule* CSchedule::NewL(CFileStore& aStore, TStreamId& aStreamId)
103 CSchedule* self = new(ELeave) CSchedule;
104 CleanupStack::PushL(self);
105 self->RestoreL(aStore, aStreamId);//get self from root
106 CleanupStack::Pop();//self
110 CSchedule::CSchedule(const TSecurityInfo& aSecurityInfo, TInt aHandle, TBool aPersists)
112 iPersists(aPersists),
114 iTaskList(TScheduledTask::Offset()),
115 iEntryList(TScheduleEntry::Offset()),
116 iSecurityInfo(aSecurityInfo)
120 CSchedule::CSchedule()
121 : iTaskList(TScheduledTask::Offset()),
122 iEntryList(TScheduleEntry::Offset())
126 CSchedule::~CSchedule()
128 LOGSTRING("CSchedule::~CSchedule - start");
135 LOGSTRING("CSchedule::~CSchedule - end");
138 void CSchedule::ConstructL(const TDesC& aName,
139 const CArrayFixFlat<TScheduleEntryInfo2>& aEntries)
141 iName = aName.AllocL();
142 AddEntriesL(aEntries);
145 void CSchedule::ConstructL(const TDesC& aName,
146 const CArrayFixFlat<TTaskSchedulerCondition>& aEntries,
147 const TTsTime& aDefaultRunTime)
149 iName = aName.AllocL();
150 AddConditionsL(aEntries);
152 // we plug the default time in as the start time of a schedule entry
153 TScheduleEntryInfo2 info(aDefaultRunTime, EDaily, 1, 60*24);
154 TScheduleEntry* entry = ScheduleEntryFactory::CreateL(info);
155 iEntryList.AddLast(*entry);
158 void CSchedule::RestoreL(CFileStore& aStore, TStreamId& aId)
160 RStoreReadStream scheduleStream;
161 scheduleStream.OpenLC(aStore, aId);
162 InternalizeL(scheduleStream);
163 CleanupStack::PopAndDestroy(&scheduleStream);
166 TInt CSchedule::Offset()
168 return (_FOFF(CSchedule, iLink));
171 TBool CSchedule::ClientInSchedule(const TDesC& aClientName)
173 // returns true if the client is part of a task associated with this schedule
176 TSglQueIter<TScheduledTask> tasks(iTaskList);
179 TScheduledTask* task;
180 while ((task=tasks++)!=NULL)
182 if (task->Client().ExecutorFileName().MatchF(aClientName) == 0)
192 void CSchedule::AddTask(TScheduledTask& aTask)
194 iTaskList.AddFirst(aTask);
197 void CSchedule::RemoveTasks(TBool aFromClient)
199 LOGSTRING("CSchedule::RemoveTasks - start");
201 TScheduledTask* task;
202 TSglQueIter<TScheduledTask> taskIter(iTaskList);
203 taskIter.SetToFirst();
204 while ((task = taskIter++) != NULL)
213 LOGSTRING("CSchedule::RemoveTasks - end");
216 void CSchedule::RemoveTask(TScheduledTask* aTask)
218 LOGSTRING("CSchedule::RemoveTask - start");
220 LOGSTRING2("CSchedule::RemoveTask - Schedule id: %d", iId);
221 iTaskList.Remove(*aTask);
223 LOGSTRING("CSchedule::RemoveTask - end");
226 void CSchedule::NotifyTasks()
228 LOGSTRING("CSchedule::NotifyTasks - start");
230 TScheduledTask* task;
231 TSglQueIter<TScheduledTask> taskIter(iTaskList);
232 taskIter.SetToFirst();
234 while((task = taskIter++) != NULL)
236 if (iDueTime.IsUtc())
237 time.SetUtcTime(iDueTime.GetUtcTime() + iValidityPeriod);
239 time.SetLocalTime(iDueTime.GetLocalTime()+iValidityPeriod);
242 if (task->Info().iRepeat > 0)
244 if (task->Info().iRepeat == 0)
248 LOGSTRING("CSchedule::NotifyTasks - end");
251 TInt CSchedule::GenerateTaskId()
253 LOGSTRING("CSchedule::GenerateTaskId - start");
256 TScheduledTask* task = Task(id);
259 if ((id-iId) > KMaxTasksPerSchedule)
264 LOGSTRING("CSchedule::GenerateTaskId - end");
268 TScheduledTask* CSchedule::Task(const TInt aTaskId)
270 TSglQueIter<TScheduledTask> tasks(iTaskList);
273 TScheduledTask* task;
274 while ((task=tasks++)!=NULL)
276 if (task->Info().iTaskId == aTaskId)
282 void CSchedule::TasksL(CArrayFixFlat<TTaskInfo>& aTasks)
284 LOGSTRING("CSchedule::TasksL - start");
285 TSglQueIter<TScheduledTask> taskIter(iTaskList);
286 taskIter.SetToFirst();
287 TScheduledTask* task;
288 while ((task = taskIter++) != NULL)
290 aTasks.AppendL(task->Info());
292 LOGSTRING("CSchedule::TasksL - end");
296 //Externalize/Internalize methods
298 void CSchedule::ExternalizeL(RWriteStream& aWriteStream) const
300 LOGSTRING("CSchedule::ExternalizeL - start");
302 aWriteStream.WriteInt32L(iId);
303 aWriteStream << *iName;
304 aWriteStream.WriteInt32L(iPersists);
305 aWriteStream.WriteInt32L(iEnabled);
308 // Count the number of schedule entries so that
309 // we can write the count (in the stream) in advance
310 // of the entries themselves.
311 TSglQueIter<TScheduleEntry> iter(iEntryList);
314 TScheduleEntry* entry;
315 while((entry=iter++)!=NULL)
317 aWriteStream.WriteInt32L(count);
321 while((entry=iter++)!=NULL)
323 entry->Info().ExternalizeL(aWriteStream);
327 count = iConditions.Count();
328 aWriteStream.WriteInt32L(count);
329 for(TInt ii = 0; ii<count; ++ii)
331 aWriteStream.WriteInt32L(iConditions[ii].iCategory.iUid);
332 aWriteStream.WriteUint32L(iConditions[ii].iKey);
333 aWriteStream.WriteInt32L(iConditions[ii].iState);
334 aWriteStream.WriteInt32L(iConditions[ii].iType);
337 //write security Info
338 aWriteStream << iSecurityInfo;
340 LOGSTRING("CSchedule::ExternalizeL - end");
343 void CSchedule::InternalizeL(RReadStream& aReadStream)
345 LOGSTRING("CSchedule::InternalizeL - start");
346 iId = aReadStream.ReadInt32L();
347 HBufC* name = HBufC::NewL(aReadStream, KMaxName);
350 iPersists = aReadStream.ReadInt32L();
351 iEnabled = aReadStream.ReadInt32L();
353 //Make sure we remove the entries first!
356 TInt entries = aReadStream.ReadInt32L();
357 for (TInt i=0; i<entries;i++)
359 TScheduleEntryInfo2 info;
360 info.InternalizeL(aReadStream);
361 TScheduleEntry* entry = ScheduleEntryFactory::CreateL(info);
362 iEntryList.AddLast(*entry);
365 // now read in conditions
367 TInt conditions = aReadStream.ReadInt32L();
368 for (TInt ii=0; ii<conditions;++ii)
371 category.iUid = aReadStream.ReadInt32L();
372 TUint key = aReadStream.ReadUint32L();
373 TInt state = aReadStream.ReadInt32L();
374 TTaskSchedulerCondition::TConditionType type
375 = static_cast<TTaskSchedulerCondition::TConditionType>(aReadStream.ReadInt32L());
376 TTaskSchedulerCondition condition(category, key, state, type);
377 User::LeaveIfError(iConditions.Append(condition));
380 // read in security Info
381 aReadStream >> iSecurityInfo;
383 LOGSTRING("CSchedule::InternalizeL - end");
390 void CSchedule::EntriesL(CArrayFixFlat<TScheduleEntryInfo2>& aEntries)
392 LOGSTRING("CSchedule::EntriesL - start");
393 TSglQueIter<TScheduleEntry> entryIter(iEntryList);
394 entryIter.SetToFirst();
395 TScheduleEntry* entry;
396 while ((entry = entryIter++) != NULL)
398 aEntries.AppendL(entry->Info());
400 LOGSTRING("CSchedule::EntriesL - end");
403 void CSchedule::AddEntriesL(const CArrayFixFlat<TScheduleEntryInfo2>& aEntries)
405 LOGSTRING("CSchedule::AddEntriesL - start");
406 TInt count = aEntries.Count();
407 TScheduleEntryInfo2 entryInfo2;
408 TTsTime ttsTime; //temporary needed due to gccxml compiler
411 for (TInt i = 0;i<count;i++)
413 entryInfo2 = aEntries.At(i);
415 // Zero out time to the nearest ms.
416 if(entryInfo2.StartTime().IsUtc())
418 dateTime = entryInfo2.StartTime().GetUtcTime().DateTime();
419 dateTime.SetMicroSecond(0);
420 ttsTime.SetUtcTime(dateTime);
421 entryInfo2.SetStartTime(ttsTime);
425 dateTime = entryInfo2.StartTime().GetLocalTime().DateTime();
426 dateTime.SetMicroSecond(0);
427 ttsTime.SetLocalTime(dateTime);
428 entryInfo2.SetStartTime(ttsTime);
431 TScheduleEntry* entry = ScheduleEntryFactory::CreateL(entryInfo2);
432 iEntryList.AddLast(*entry);
435 LOGSTRING("CSchedule::AddEntriesL - end");
438 void CSchedule::ReplaceEntriesL(const CArrayFixFlat<TScheduleEntryInfo2>& aEntries)
440 // remove the original entries
442 AddEntriesL(aEntries);
445 void CSchedule::RemoveEntries()
447 LOGSTRING("CSchedule::RemoveEntries - start");
449 TScheduleEntry* entry;
450 TSglQueIter<TScheduleEntry> iter(iEntryList);
452 while ((entry = iter++) != NULL)
454 iEntryList.Remove(*entry);
458 LOGSTRING("CSchedule::RemoveEntries - end");
462 void CSchedule::AddConditionsL(const CArrayFixFlat<TTaskSchedulerCondition>& aConditions)
464 TInt count = aConditions.Count();
465 for (TInt i = 0;i<count;++i)
466 User::LeaveIfError(iConditions.Append(aConditions[i]));
469 void CSchedule::ReplaceConditionsL(const CArrayFixFlat<TTaskSchedulerCondition>& aConditions)
471 // remove the original conditions
473 AddConditionsL(aConditions);
476 void CSchedule::ConditionsL(CArrayFixFlat<TTaskSchedulerCondition>& aConditions)
478 TInt count = iConditions.Count();
479 for (TInt i = 0;i<count;++i)
481 aConditions.AppendL(iConditions[i]);
485 void CSchedule::RemoveConditions()
490 const TTsTime& CSchedule::DefaultRunTimeL() const
492 TSglQueIter<TScheduleEntry> entryIter(iEntryList);
493 entryIter.SetToFirst();
494 TScheduleEntry* entry = entryIter;
496 User::Leave(KErrArgument);
497 return entry->DueTime();
500 // This method is called when ever a new task is scheduled or a change to an
501 // existing schedule is made. When one of the schedule entries in this schedule
502 // has become due, this is called with aNotFirstTime = ETrue. All this does is
503 // move the next due time into the next time frame.
504 void CSchedule::CalculateDueTime(TBool aNotFirstTime)
506 // Sort the list of entries
507 TSglQueIter<TScheduleEntry> iter(iEntryList);
509 TScheduleEntry* entry;
511 TTsTime currentTTsTime;
512 //make sure we reset iDueTime to max so that only the minimum is calculated.
513 TTsTime maxTime(Time::MaxTTime(), ETrue);
516 while ((entry = iter++)!=NULL)
518 currentTTime.UniversalTime();
519 currentTTsTime.SetUtcTime(currentTTime);
520 // This works out when the schedule is next due based on the input time
521 // and also updates the due time if it is home time based
522 dueTime = entry->NextScheduledTime(currentTTsTime);
523 if(aNotFirstTime && dueTime.GetUtcTime() <= currentTTime)
525 // We don't want this schedule to run straight away so seed the
526 // next due initial time-frame by incrementing the validity
527 currentTTime += entry->Info().ValidityPeriod();
528 currentTTime += TTimeIntervalMicroSeconds32(1); // push into the next boundary
531 currentTTsTime.SetUtcTime(currentTTime);
533 currentTTsTime.SetLocalTime(currentTTime + dueTime.GetOffset());
535 dueTime = entry->NextScheduledTime(currentTTsTime);
537 if(dueTime.GetUtcTime() < iDueTime.GetUtcTime()) //find earliest due time from all entries
540 iValidityPeriod = entry->Info().ValidityPeriod();
545 // if aCalculateForConditions is true then entrycount corresponds
546 // to number of conditions;
547 void CSchedule::GetInfo(TScheduleInfo& aInfo, TBool aCalculateForConditions)
549 aInfo.iState.SetName(Name());
550 aInfo.iState.SetDueTime(iDueTime);
551 aInfo.iState.SetPersists(Persists());
552 aInfo.iState.SetEnabled(Enabled());
555 TSglQueIter<TScheduledTask> taskIter(*Tasks());
556 taskIter.SetToFirst();
557 while (taskIter++ != NULL)
561 aInfo.iTaskCount = taskCount;
563 if(!aCalculateForConditions)
565 TSglQueIter<TScheduleEntry> entryIter(iEntryList);
566 entryIter.SetToFirst();
567 while (entryIter++ != NULL)
573 entryCount = iConditions.Count();
574 aInfo.iEntryCount = entryCount;
577 const RArray<TTaskSchedulerCondition>& CSchedule::Conditions() const
582 TBool CSchedule::IsAccessAllowed(const RMessagePtr2& aMessage) const
584 // Access allowed if message SID is the same as the schedule creator
585 // or if client has WriteDeviceData
586 return aMessage.SecureId()==iSecurityInfo.iSecureId
587 || aMessage.HasCapability(ECapabilityWriteDeviceData)
588 || PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)==0; // Enforcement off
591 const TSecurityInfo& CSchedule::SecurityInfo() const
593 return iSecurityInfo;
596 TBool CSchedule::IsUpdatable()
598 if(HasTasks() && Enabled() )