forked from ical-org/ical.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlarm.cs
More file actions
178 lines (156 loc) · 6.03 KB
/
Alarm.cs
File metadata and controls
178 lines (156 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using Ical.Net.DataTypes;
namespace Ical.Net.CalendarComponents
{
/// <summary>
/// A class that represents an RFC 2445 VALARM component.
/// FIXME: move GetOccurrences() logic into an AlarmEvaluator.
/// </summary>
public class Alarm : CalendarComponent
{
//ToDo: Implement IEquatable
public string Action
{
get => Properties.Get<string>(AlarmAction.Key);
set => Properties.Set(AlarmAction.Key, value);
}
public Attachment Attachment
{
get => Properties.Get<Attachment>("ATTACH");
set => Properties.Set("ATTACH", value);
}
public IList<Attendee> Attendees
{
get => Properties.GetMany<Attendee>("ATTENDEE");
set => Properties.Set("ATTENDEE", value);
}
public string Description
{
get => Properties.Get<string>("DESCRIPTION");
set => Properties.Set("DESCRIPTION", value);
}
public TimeSpan Duration
{
get => Properties.Get<TimeSpan>("DURATION");
set => Properties.Set("DURATION", value);
}
public int Repeat
{
get => Properties.Get<int>("REPEAT");
set => Properties.Set("REPEAT", value);
}
public string Summary
{
get => Properties.Get<string>("SUMMARY");
set => Properties.Set("SUMMARY", value);
}
public Trigger? Trigger
{
get => Properties.Get<Trigger>(TriggerRelation.Key);
set => Properties.Set(TriggerRelation.Key, value);
}
protected IList<AlarmOccurrence> Occurrences { get; set; }
public Alarm()
{
Name = Components.Alarm;
Occurrences = new List<AlarmOccurrence>();
}
/// <summary>
/// Gets a list of alarm occurrences for the given recurring component, <paramref name="rc"/>
/// that occur between <paramref name="fromDate"/> and <paramref name="toDate"/>.
/// </summary>
public IList<AlarmOccurrence> GetOccurrences(IRecurringComponent rc, IDateTime fromDate, IDateTime toDate)
{
Occurrences.Clear();
if (Trigger == null)
{
return Occurrences;
}
// If the trigger is relative, it can recur right along with
// the recurring items, otherwise, it happens once and
// only once (at a precise time).
if (Trigger.IsRelative)
{
// Ensure that "FromDate" has already been set
if (fromDate == null)
{
fromDate = rc.Start.Copy<IDateTime>();
}
TimeSpan? d = default;
foreach (var o in rc.GetOccurrences(fromDate, toDate))
{
var dt = o.Period.StartTime;
if (string.Equals(Trigger.Related, TriggerRelation.End, TriggerRelation.Comparison))
{
if (o.Period.EndTime != null)
{
dt = o.Period.EndTime;
if (d == default)
{
d = o.Period.Duration;
}
}
// Use the "last-found" duration as a reference point
else if (d != default)
{
dt = o.Period.StartTime.Add(d.Value);
}
else
{
throw new ArgumentException(
"Alarm trigger is relative to the START of the occurrence; however, the occurrence has no discernible end.");
}
}
Occurrences.Add(new AlarmOccurrence(this, dt.Add(Trigger.Duration.Value), rc));
}
}
else
{
var dt = Trigger.DateTime.Copy<IDateTime>();
dt.AssociatedObject = this;
Occurrences.Add(new AlarmOccurrence(this, dt, rc));
}
// If a REPEAT and DURATION value were specified,
// then handle those repetitions here.
AddRepeatedItems();
return Occurrences;
}
/// <summary>
/// Polls the <see cref="Alarm"/> component for alarms that have been triggered
/// between <paramref name="start"/> and <paramref name="end"/> date/time.
/// If <paramref name="start"/> is null, all triggered alarms will be returned.
/// </summary>
/// <returns>A list of <see cref="AlarmOccurrence"/> objects, each containing a triggered alarm.</returns>
public IList<AlarmOccurrence> Poll(IDateTime start, IDateTime end)
{
var results = new List<AlarmOccurrence>();
// Evaluate the alarms to determine the recurrences
if (!(Parent is RecurringComponent rc))
{
return results;
}
results.AddRange(GetOccurrences(rc, start, end));
return results;
}
/// <summary>
/// Handles the repetitions that occur from the <c>REPEAT</c> and
/// <c>DURATION</c> properties. Each recurrence of the alarm will
/// have its own set of generated repetitions.
/// </summary>
protected virtual void AddRepeatedItems()
{
var len = Occurrences.Count;
for (var i = 0; i < len; i++)
{
var ao = Occurrences[i];
var alarmTime = ao.DateTime.Copy<IDateTime>();
for (var j = 0; j < Repeat; j++)
{
alarmTime = alarmTime.Add(Duration);
Occurrences.Add(new AlarmOccurrence(this, alarmTime.Copy<IDateTime>(), ao.Component));
}
}
}
}
}