|
1 /* |
|
2 * Copyright (c) 2008 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 "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 * Functions to calculate timer intervals |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "emailtrace.h" |
|
21 #include "IpsSosAOSchedulerUtils.h" |
|
22 #include "IpsSetDataExtension.h" |
|
23 |
|
24 |
|
25 |
|
26 const TInt KAOSecondsIn24Hours = 86400; |
|
27 //const TUint KAOEveryDayMask = 0x7f; |
|
28 const TInt KA0DaysInWeek = 7; |
|
29 |
|
30 // ---------------------------------------------------------------------------- |
|
31 // ---------------------------------------------------------------------------- |
|
32 // |
|
33 CIpsSosAOSchedulerUtils::CIpsSosAOSchedulerUtils( |
|
34 const CIpsSetDataExtension& aExtentedSettings ) |
|
35 : iExtentedSettings( aExtentedSettings ) |
|
36 { |
|
37 FUNC_LOG; |
|
38 } |
|
39 |
|
40 // ---------------------------------------------------------------------------- |
|
41 // ---------------------------------------------------------------------------- |
|
42 // |
|
43 CIpsSosAOSchedulerUtils::~CIpsSosAOSchedulerUtils() |
|
44 { |
|
45 FUNC_LOG; |
|
46 } |
|
47 |
|
48 // ---------------------------------------------------------------------------- |
|
49 // ---------------------------------------------------------------------------- |
|
50 // |
|
51 void CIpsSosAOSchedulerUtils::ConstructL() |
|
52 { |
|
53 FUNC_LOG; |
|
54 } |
|
55 |
|
56 // ---------------------------------------------------------------------------- |
|
57 // ---------------------------------------------------------------------------- |
|
58 // |
|
59 CIpsSosAOSchedulerUtils* CIpsSosAOSchedulerUtils::NewL( |
|
60 const CIpsSetDataExtension& aExtentedSettings ) |
|
61 { |
|
62 FUNC_LOG; |
|
63 CIpsSosAOSchedulerUtils* self = NewLC( aExtentedSettings ); |
|
64 CleanupStack::Pop( self ); |
|
65 |
|
66 return self; |
|
67 } |
|
68 |
|
69 // ---------------------------------------------------------------------------- |
|
70 // ---------------------------------------------------------------------------- |
|
71 // |
|
72 CIpsSosAOSchedulerUtils* CIpsSosAOSchedulerUtils::NewLC( |
|
73 const CIpsSetDataExtension& aExtentedSettings ) |
|
74 { |
|
75 FUNC_LOG; |
|
76 CIpsSosAOSchedulerUtils* self = |
|
77 new ( ELeave ) CIpsSosAOSchedulerUtils( aExtentedSettings ); |
|
78 CleanupStack::PushL( self ); |
|
79 self->ConstructL(); |
|
80 |
|
81 return self; |
|
82 } |
|
83 |
|
84 // ---------------------------------------------------------------------------- |
|
85 // ---------------------------------------------------------------------------- |
|
86 // |
|
87 TBool CIpsSosAOSchedulerUtils::CanConnect() |
|
88 { |
|
89 FUNC_LOG; |
|
90 |
|
91 // Get hometime |
|
92 TTime clock; |
|
93 TTimeIntervalSeconds homeTime; |
|
94 TTimeIntervalSeconds startTime; |
|
95 TTimeIntervalSeconds stopTime; |
|
96 PrepareScheduling( clock, homeTime, startTime, stopTime ); |
|
97 |
|
98 |
|
99 TBool ok = CanConnect( clock, homeTime, startTime, stopTime ); |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 return ok; |
|
105 } |
|
106 |
|
107 // ---------------------------------------------------------------------------- |
|
108 // ---------------------------------------------------------------------------- |
|
109 // |
|
110 TIpsSosAOConnectionType CIpsSosAOSchedulerUtils::SecondsToNextMark( |
|
111 TTimeIntervalSeconds& aSeconds ) |
|
112 { |
|
113 FUNC_LOG; |
|
114 |
|
115 // Get hometime |
|
116 aSeconds = 0; |
|
117 TTime clock; |
|
118 TTimeIntervalSeconds homeTime; |
|
119 TTimeIntervalSeconds startTime; |
|
120 TTimeIntervalSeconds stopTime; |
|
121 PrepareScheduling( clock, homeTime, startTime, stopTime ); |
|
122 |
|
123 // At first it needs to be checked, that either previous or current day |
|
124 // is selected. |
|
125 TIpsSosAOValidConnectionDay validDay = GetValidConnectionDay( clock ); |
|
126 TBool timeToConnect = ( validDay != EAOVCDNoConnection ); |
|
127 |
|
128 // Next step is to check, is it time for connection or disconnection |
|
129 if ( timeToConnect ) |
|
130 { |
|
131 timeToConnect = IsValidTimeToConnect( |
|
132 validDay, homeTime, startTime, stopTime ); |
|
133 } |
|
134 |
|
135 |
|
136 // Make the conversion |
|
137 TIpsSosAOConnectionType connectStatus; |
|
138 |
|
139 if ( timeToConnect ) |
|
140 { |
|
141 connectStatus = EAOCDisconnectAfter; |
|
142 } |
|
143 else |
|
144 { |
|
145 connectStatus = EAOCConnectAfter; |
|
146 } |
|
147 |
|
148 |
|
149 // Finally calculate the time to next mark |
|
150 CalculateSecondsToNextMark( |
|
151 connectStatus, aSeconds, clock, homeTime, startTime, stopTime ); |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 return connectStatus; |
|
157 } |
|
158 |
|
159 // ---------------------------------------------------------------------------- |
|
160 // ---------------------------------------------------------------------------- |
|
161 // |
|
162 void CIpsSosAOSchedulerUtils::PrepareScheduling( |
|
163 TTime& aClock, |
|
164 TTimeIntervalSeconds& aHome, |
|
165 TTimeIntervalSeconds& aStart, |
|
166 TTimeIntervalSeconds& aStop ) |
|
167 { |
|
168 FUNC_LOG; |
|
169 // Get the hometime and set the day flags |
|
170 aClock.HomeTime(); |
|
171 |
|
172 // First, all the times has to be converted to seconds, to guarrantee |
|
173 // proper comparisions for the times |
|
174 Times2Seconds( aClock, aHome, aStart, aStop ); |
|
175 |
|
176 // Start and stop times needs to be reordered so, that the |
|
177 // start time is before stop time in proportion to hometime |
|
178 OrganizeStartAndStopTimes( aHome, aStart, aStop ); |
|
179 } |
|
180 |
|
181 // ---------------------------------------------------------------------------- |
|
182 // ---------------------------------------------------------------------------- |
|
183 // |
|
184 TBool CIpsSosAOSchedulerUtils::CanConnect( |
|
185 const TTime& aClock, |
|
186 TTimeIntervalSeconds& aHome, |
|
187 TTimeIntervalSeconds& aStart, |
|
188 TTimeIntervalSeconds& aStop ) |
|
189 { |
|
190 FUNC_LOG; |
|
191 |
|
192 TIpsSosAOValidConnectionDay validDay = GetValidConnectionDay( aClock ); |
|
193 TBool result = EFalse; |
|
194 |
|
195 // Connecting can be ok, if either previous or current day are checked |
|
196 if ( validDay != EAOVCDNoConnection ) |
|
197 { |
|
198 |
|
199 result = IsValidTimeToConnect( |
|
200 validDay, aHome, aStart, aStop ); |
|
201 } |
|
202 |
|
203 |
|
204 return result; |
|
205 } |
|
206 |
|
207 // ---------------------------------------------------------------------------- |
|
208 // The connection is allowed in following cases: |
|
209 // Day: Mo Tu We Th Fr Sa Su |
|
210 // Checked: 0 0 0 1 1 0 0 |
|
211 // Allowed: F F F T T T F |
|
212 // ---------------------------------------------------------------------------- |
|
213 // |
|
214 TIpsSosAOValidConnectionDay CIpsSosAOSchedulerUtils::GetValidConnectionDay( |
|
215 const TTime& aClock ) |
|
216 { |
|
217 FUNC_LOG; |
|
218 |
|
219 // Get current and previous day and make sure it is selected |
|
220 TDay currentDay = aClock.DayNoInWeek(); |
|
221 TDay previousDay = GetPrevDay( currentDay ); |
|
222 TIpsSosAOValidConnectionDay result; |
|
223 |
|
224 |
|
225 // Connection can be made anyday |
|
226 if ( IsDaySelected( currentDay ) && IsDaySelected( previousDay ) ) |
|
227 { |
|
228 |
|
229 result = EAOVCDConnectionAnyDay; |
|
230 } |
|
231 // Connection can be established when start time is at current day |
|
232 else if ( IsDaySelected( currentDay ) ) |
|
233 { |
|
234 |
|
235 result = EAOVCDConnectionCurrentDay; |
|
236 } |
|
237 // Connection can be established, if the connection begins from |
|
238 // checked day |
|
239 else if ( IsDaySelected( previousDay ) ) |
|
240 { |
|
241 |
|
242 result = EAOVCDConnectionPreviousDayOnly; |
|
243 } |
|
244 // no selected days this is wron in settings side but still return anyday |
|
245 else if ( iExtentedSettings.SelectedWeekDays() == 0 ) |
|
246 { |
|
247 result = EAOVCDConnectionAnyDay; |
|
248 } |
|
249 // Connection is not allowed |
|
250 else |
|
251 { |
|
252 |
|
253 result = EAOVCDNoConnection; |
|
254 } |
|
255 |
|
256 return result; |
|
257 } |
|
258 |
|
259 // ----------------------------------------------------------------------------) |
|
260 // ---------------------------------------------------------------------------- |
|
261 // |
|
262 TDay CIpsSosAOSchedulerUtils::GetNextDay( |
|
263 const TDay aToday, |
|
264 const TInt aNth ) |
|
265 { |
|
266 FUNC_LOG; |
|
267 return static_cast<TDay>( ( aToday + aNth ) % KA0DaysInWeek ); |
|
268 } |
|
269 |
|
270 // ---------------------------------------------------------------------------- |
|
271 // ---------------------------------------------------------------------------- |
|
272 // |
|
273 TDay CIpsSosAOSchedulerUtils::GetPrevDay( |
|
274 const TDay aToday, |
|
275 const TInt aNth ) |
|
276 { |
|
277 FUNC_LOG; |
|
278 TInt weekIncrease = ( ( aNth / KA0DaysInWeek ) + 1 ) * KA0DaysInWeek; |
|
279 return static_cast<TDay>( ( weekIncrease + aToday - aNth ) % KA0DaysInWeek ); |
|
280 } |
|
281 |
|
282 // ---------------------------------------------------------------------------- |
|
283 // ---------------------------------------------------------------------------- |
|
284 // |
|
285 TBool CIpsSosAOSchedulerUtils::IsValidTimeToConnect( |
|
286 const TIpsSosAOValidConnectionDay aValidDay, |
|
287 TTimeIntervalSeconds& aHome, |
|
288 TTimeIntervalSeconds& aStart, |
|
289 TTimeIntervalSeconds& aStop ) |
|
290 { |
|
291 FUNC_LOG; |
|
292 |
|
293 TBool result = EFalse; |
|
294 |
|
295 // Combine the possible combinations. |
|
296 // Connection in current day is possible, only if the current day or |
|
297 // all days are selected |
|
298 TBool currentDay = ( aValidDay == EAOVCDConnectionCurrentDay || |
|
299 aValidDay == EAOVCDConnectionAnyDay ); |
|
300 // Connection can extended from previous day, only if previous day is |
|
301 // set or all days are set |
|
302 TBool previousDay = ( aValidDay == EAOVCDConnectionPreviousDayOnly || |
|
303 aValidDay == EAOVCDConnectionAnyDay ); |
|
304 |
|
305 // First it is needed to check that are the times even set |
|
306 // If the times are equal, the times can be considered to be valid for |
|
307 // all the day |
|
308 if ( currentDay && aStart == aStop ) |
|
309 { |
|
310 result = ETrue; |
|
311 } |
|
312 // It's allowed to make the connection in following situations homeTime |
|
313 // is between startTime and stopTime, and the connection is allowed |
|
314 // for the day. The connection is not allowed to be open during |
|
315 // the last minute (aHome < aStop), but connection should be |
|
316 // made at the start of the scheduling (aStart <= aHome). |
|
317 else if ( aStart <= aHome && aHome < aStop ) |
|
318 { |
|
319 |
|
320 // If connection extends from previous day and only extended connection |
|
321 // is allowed |
|
322 if ( previousDay && aStart.Int() < 0 ) |
|
323 { |
|
324 |
|
325 result = ETrue; |
|
326 } |
|
327 // If previous day is not checked and connection extends over day |
|
328 // make sure that the correct time is used |
|
329 else if ( currentDay && aStart.Int() >= 0 ) |
|
330 { |
|
331 |
|
332 result = ETrue; |
|
333 } |
|
334 else |
|
335 { |
|
336 |
|
337 result = EFalse; |
|
338 } |
|
339 } |
|
340 // Neither of the statements were fulfilled, do not allow connection |
|
341 else |
|
342 { |
|
343 |
|
344 result = EFalse; |
|
345 } |
|
346 |
|
347 return result; |
|
348 } |
|
349 |
|
350 // ---------------------------------------------------------------------------- |
|
351 // ---------------------------------------------------------------------------- |
|
352 // |
|
353 TDateTime CIpsSosAOSchedulerUtils::AdjustHomeTime( |
|
354 const TTime& aClock ) |
|
355 { |
|
356 FUNC_LOG; |
|
357 // Zero time is the beginning of the day |
|
358 TTime zeroTime( 0 ); |
|
359 |
|
360 TDateTime dtHome = aClock.DateTime(); |
|
361 TDateTime dtZero = zeroTime.DateTime(); |
|
362 |
|
363 dtZero.SetHour( dtHome.Hour() ); |
|
364 dtZero.SetMinute( dtHome.Minute() ); |
|
365 dtZero.SetSecond( dtHome.Second() ); |
|
366 |
|
367 return dtZero; |
|
368 } |
|
369 |
|
370 // ---------------------------------------------------------------------------- |
|
371 // This function converts home-, start- and stop times to secondes from the |
|
372 // beginning of the day |
|
373 // ---------------------------------------------------------------------------- |
|
374 // |
|
375 void CIpsSosAOSchedulerUtils::Times2Seconds( |
|
376 const TTime& aClock, |
|
377 TTimeIntervalSeconds& aHome, |
|
378 TTimeIntervalSeconds& aStart, |
|
379 TTimeIntervalSeconds& aStop ) |
|
380 { |
|
381 FUNC_LOG; |
|
382 |
|
383 TTime zeroTime( 0 ); |
|
384 TTime adjustedHomeTime = AdjustHomeTime( aClock ); |
|
385 TTime selectedTimeStart = iExtentedSettings.SelectedTimeStart(); |
|
386 TTime selectedTimeStop = iExtentedSettings.SelectedTimeStop(); |
|
387 adjustedHomeTime.SecondsFrom( zeroTime, aHome ); |
|
388 selectedTimeStart.SecondsFrom( zeroTime, aStart ); |
|
389 selectedTimeStop.SecondsFrom( zeroTime, aStop ); |
|
390 |
|
391 } |
|
392 |
|
393 // ---------------------------------------------------------------------------- |
|
394 // ---------------------------------------------------------------------------- |
|
395 // |
|
396 void CIpsSosAOSchedulerUtils::OrganizeStartAndStopTimes( |
|
397 TTimeIntervalSeconds& aHome, |
|
398 TTimeIntervalSeconds& aStart, |
|
399 TTimeIntervalSeconds& aStop ) |
|
400 { |
|
401 FUNC_LOG; |
|
402 |
|
403 // The start-, stop- and home times needs to be reordered so that the |
|
404 // start is always before stop, in other words, start time is smaller |
|
405 // than stop time. |
|
406 // |
|
407 // Following cases need to be considered: |
|
408 // aStart < aStop (OK) - aStart > aStop (NOK) |
|
409 // aHome < aStart < aStop ( aHome < aStop ) < aStart (start-24h) |
|
410 // aStart < aHome < aStop aStop < aHome < aStart (end+24h) |
|
411 // aStart < aStop < aHome aStop < aStart < aHome (end+24h) |
|
412 if ( aStart > aStop ) |
|
413 { |
|
414 // If we're between the the start and stop times, |
|
415 // move this |
|
416 if ( aHome < aStop ) |
|
417 { |
|
418 // Set starting time to previous day |
|
419 aStart = ( aStart.Int() - KAOSecondsIn24Hours ); |
|
420 } |
|
421 else |
|
422 { |
|
423 // Set ending time to next day |
|
424 aStop = ( aStop.Int() + KAOSecondsIn24Hours ); |
|
425 } |
|
426 } |
|
427 |
|
428 |
|
429 } |
|
430 |
|
431 // ---------------------------------------------------------------------------- |
|
432 // ---------------------------------------------------------------------------- |
|
433 // |
|
434 void CIpsSosAOSchedulerUtils::CalculateSecondsToNextMark( |
|
435 const TIpsSosAOConnectionType aConnectionStatus, |
|
436 TTimeIntervalSeconds& aSeconds, |
|
437 const TTime& aClock, |
|
438 const TTimeIntervalSeconds& aHome, |
|
439 const TTimeIntervalSeconds& aStart, |
|
440 const TTimeIntervalSeconds& aStop ) |
|
441 { |
|
442 FUNC_LOG; |
|
443 |
|
444 // If all day is selected, next stop should be the midnight of the next |
|
445 // (un)scheduled day depending on should the connection be on or off |
|
446 if ( aStart == aStop ) |
|
447 { |
|
448 |
|
449 aSeconds = CalcSecsToNextScheduledDay( |
|
450 aClock, aHome, aConnectionStatus == EAOCConnectAfter ); |
|
451 |
|
452 } |
|
453 // To calculate seconds, it needs to checked, should the connection be now |
|
454 // on or off. If between, the connection should be on and seconds should |
|
455 // be calculated to next disconnect time |
|
456 else if ( aConnectionStatus == EAOCDisconnectAfter ) |
|
457 { |
|
458 |
|
459 CalcSecsToMark( aSeconds, aClock, aHome, aStop, EFalse ); |
|
460 |
|
461 } |
|
462 // |
|
463 else |
|
464 { |
|
465 |
|
466 CalcSecsToMark( aSeconds, aClock, aHome, aStart, ETrue ); |
|
467 |
|
468 } |
|
469 |
|
470 } |
|
471 |
|
472 // ---------------------------------------------------------------------------- |
|
473 // ---------------------------------------------------------------------------- |
|
474 // |
|
475 void CIpsSosAOSchedulerUtils::CalcSecsToMark( |
|
476 TTimeIntervalSeconds& aSeconds, |
|
477 const TTime& aClock, |
|
478 const TTimeIntervalSeconds& aHome, |
|
479 const TTimeIntervalSeconds& aBonus, |
|
480 const TBool aScheduledDay ) |
|
481 { |
|
482 FUNC_LOG; |
|
483 // If extending to next day, calculate seconds to next day |
|
484 if ( aHome > aBonus ) |
|
485 { |
|
486 aSeconds = CalcSecsToNextScheduledDay( aClock, aHome, aScheduledDay ); |
|
487 aSeconds = aSeconds.Int() + aBonus.Int(); |
|
488 } |
|
489 else |
|
490 { |
|
491 aSeconds = aBonus.Int() - aHome.Int(); |
|
492 } |
|
493 } |
|
494 |
|
495 // ---------------------------------------------------------------------------- |
|
496 // ---------------------------------------------------------------------------- |
|
497 // |
|
498 TTimeIntervalSeconds CIpsSosAOSchedulerUtils::CalcSecsToNextScheduledDay( |
|
499 const TTime& aClock, |
|
500 const TTimeIntervalSeconds& aHome, |
|
501 const TBool aScheduledDay ) |
|
502 { |
|
503 FUNC_LOG; |
|
504 |
|
505 TTimeIntervalSeconds seconds = 0; |
|
506 |
|
507 // First check if all of the days are unchecked, which should not happen |
|
508 // at all, but just in case |
|
509 if ( !iExtentedSettings.SelectedWeekDays() ) |
|
510 { |
|
511 // Temporarly set all the days selected |
|
512 return seconds; |
|
513 /* __ASSERT_DEBUG( ( |
|
514 EFalse ), |
|
515 User::Panic( _L("AO"), KErrGeneral) );*/ |
|
516 } |
|
517 |
|
518 TBool hit = EFalse; |
|
519 TDay today = aClock.DayNoInWeek(); |
|
520 TDay dayAfter = GetNextDay( today ); |
|
521 TInt dayCount = KErrNotFound; |
|
522 |
|
523 while ( dayAfter != today && !hit ) |
|
524 { |
|
525 dayCount++; |
|
526 hit = ( IsDaySelected( dayAfter ) == aScheduledDay ); |
|
527 dayAfter = GetNextDay( dayAfter ); |
|
528 } |
|
529 |
|
530 |
|
531 seconds = KAOSecondsIn24Hours - aHome.Int() + |
|
532 dayCount * KAOSecondsIn24Hours; |
|
533 |
|
534 |
|
535 return seconds; |
|
536 } |
|
537 |
|
538 // ---------------------------------------------------------------------------- |
|
539 // ---------------------------------------------------------------------------- |
|
540 // |
|
541 TBool CIpsSosAOSchedulerUtils::IsDaySelected( const TUint aDay ) const |
|
542 { |
|
543 FUNC_LOG; |
|
544 return ( iExtentedSettings.SelectedWeekDays() >> aDay ) & 0x01; |
|
545 } |
|
546 |
|
547 // End of File |
|
548 |