summaryrefslogtreecommitdiffstats
path: root/src/khevent.rs
blob: 1ec9aae7de439853dad5c7a6bdadd63f124380a7 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use std::path::{Path, PathBuf};

use crate::icalwrap::IcalComponent;
use crate::icalwrap::IcalDuration;
use crate::icalwrap::IcalProperty;
use crate::icalwrap::IcalTime;
use crate::icalwrap::IcalTimeZone;
use crate::icalwrap::IcalVCalendar;
use crate::icalwrap::IcalVEvent;
use crate::KhResult;

pub struct KhEvent {
  event: IcalVEvent,
  instance_timestamp: Option<IcalTime>,
}

impl KhEvent {
  pub fn get_start(&self) -> Option<IcalTime> {
    //TODO: should probably depend on is_recur_master, not the instance timestamp
    match self.instance_timestamp {
      Some(ref timestamp) => Some(timestamp.clone()),
      None => self.event.get_dtstart(),
    }
  }

  pub fn get_end(&self) -> Option<IcalTime> {
    //TODO: should probably depend on is_recur_master, not the instance timestamp
    match self.instance_timestamp {
      Some(ref timestamp) => {
        let dur = self.get_duration().unwrap();
        let dtend = timestamp.to_owned() + dur;
        Some(dtend)
      }
      None => self.event.get_dtend(),
    }
  }

  pub fn with_internal_timestamp(&self, timestamp: &IcalTime) -> Self {
    Self {
      event: self.event.shallow_copy(),
      instance_timestamp: Some(timestamp.clone()),
    }
  }

  pub fn get_calendar_name(&self) -> Option<String> {
    self
      .event
      .get_parent()
      .and_then(|cal| cal.get_calendar_name())
  }

  pub fn get_path(&self) -> Option<&PathBuf> {
    self.event.get_parent()?.get_path()
  }

  pub fn is_allday(&self) -> bool {
    self.event.is_allday()
  }

  pub fn get_duration(&self) -> Option<IcalDuration> {
    self.event.get_duration()
  }

  pub fn get_summary(&self) -> Option<String> {
    self.event.get_summary()
  }

  pub fn get_description(&self) -> Option<String> {
    self.event.get_description()
  }

  pub fn get_location(&self) -> Option<String> {
    self.event.get_location()
  }

  pub fn get_uid(&self) -> String {
    self.event.get_uid()
  }

  pub fn get_dtstamp(&self) -> Option<String> {
    let dtstamp_kind = ical::icalproperty_kind_ICAL_DTSTAMP_PROPERTY;
    self
      .event
      .get_property(dtstamp_kind)
      .map(|prop| prop.get_value())
  }

  pub fn get_last_modified(&self) -> Option<String> {
    let last_modified_kind = ical::icalproperty_kind_ICAL_LASTMODIFIED_PROPERTY;
    self
      .event
      .get_property(last_modified_kind)
      .map(|prop| prop.get_value())
  }

  pub fn get_last_relevant_date(&self) -> Option<IcalTime> {
    //TODO this is still wrong
    //events can end at 00:00
    if self.is_allday() {
      self.get_end().map(|dtend| dtend.pred())
    } else {
      self.get_end().map(|dtend| dtend)
    }
  }

  pub fn is_recur_master(&self) -> bool {
    self.event.has_property_rrule() && self.instance_timestamp.is_none()
  }

  pub fn is_recur_valid(&self) -> bool {
    if self.is_recur_master() {
      true
    } else if let Some(ref timestamp) = self.instance_timestamp {
      let recur_times = self.event.get_recur_datetimes();
      recur_times.contains(timestamp)
    } else {
      self.instance_timestamp.is_none()
    }
  }

  pub fn get_recur_instances(&self) -> impl Iterator<Item = KhEvent> + '_ {
    self
      .get_recur_datetimes()
      .into_iter()
      .map(|recur_utc| recur_utc.with_timezone(&IcalTimeZone::local()))
      .map(move |recur_local| self.with_internal_timestamp(&recur_local))
  }

  pub fn get_recur_datetimes(&self) -> Vec<IcalTime> {
    self.event.get_recur_datetimes()
  }

  pub fn get_properties_by_name(&self, property_name: &str) -> Vec<IcalProperty> {
    self.event.get_properties_by_name(property_name)
  }

  pub fn from_event(event: IcalVEvent) -> Self {
    Self {
      event,
      instance_timestamp: None,
    }
  }

  pub fn from_event_with_timestamp(
    event: IcalVEvent,
    instance_timestamp: Option<IcalTime>,
  ) -> Self {
    Self {
      event,
      instance_timestamp,
    }
  }

  pub fn from_str(input: &str, path: Option<&Path>) -> KhResult<Self> {
    let cal = IcalVCalendar::from_str(input, path)?;
    Ok(cal.get_principal_khevent())
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::icalwrap::IcalTimeZone;
  use crate::testdata;

  #[test]
  fn test_is_recur_valid_master() {
    let event = KhEvent::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();

    assert!(event.is_recur_valid());
  }

  #[test]
  fn test_is_recur_valid_dtstart() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();
    let event = cal.get_principal_khevent();
    let start = &event.get_start().unwrap();

    let event = event.with_internal_timestamp(start);

    assert!(event.is_recur_valid());
  }

  #[test]
  fn test_is_recur_valid_incorrect() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();
    let event = cal.get_principal_khevent();

    let event = event.with_internal_timestamp(&IcalTime::floating_ymd(2010, 01, 01));

    assert!(!event.is_recur_valid());
  }

  #[test]
  fn test_is_recur_valid_other() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();
    let event = cal.get_principal_khevent();

    let event = event.with_internal_timestamp(&IcalTime::floating_ymd(2018, 10, 25));

    assert!(event.is_recur_valid());
  }

  #[test]
  fn test_is_recur_valid_nonrecur() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_ONE_MEETING, None).unwrap();
    let event = cal.get_principal_khevent();

    assert!(event.is_recur_valid());
  }

  #[test]
  fn test_is_recur_master_instance() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();
    let event = cal.get_principal_khevent();
    let event = event.with_internal_timestamp(&IcalTime::floating_ymd(2018, 01, 01));
    assert!(!event.is_recur_master());
  }

  #[test]
  fn test_is_recur_master() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();
    assert!(cal.get_principal_khevent().is_recur_master());
  }

  #[test]
  fn test_is_recur_master_invalid() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_ONE_MEETING, None).unwrap();
    assert!(!cal.get_principal_khevent().is_recur_master());
  }

  #[test]
  fn recur_datetimes_test() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_RECUR, None).unwrap();

    let event = cal.get_principal_khevent();
    let mut recur_instances = event.get_recur_instances();
    let local = IcalTimeZone::local();
    assert_eq!(
      IcalTime::floating_ymd(2018, 10, 11).with_timezone(&local),
      recur_instances.next().unwrap().get_start().unwrap()
    );
    assert_eq!(
      IcalTime::floating_ymd(2018, 10, 18).with_timezone(&local),
      recur_instances.next().unwrap().get_start().unwrap()
    );
  }

}