summaryrefslogtreecommitdiffstats
path: root/src/icalwrap/icalvevent.rs
blob: f7cc8c96f0b2125c6feb4ebf5f9d6f6c13d69f5c (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use std::ffi::CStr;

use super::IcalComponent;
use super::IcalVCalendar;
use super::IcalTime;
use super::IcalTimeZone;
use super::IcalDuration;
use crate::ical;

pub struct IcalVEvent {
  ptr: *mut ical::icalcomponent,
  parent: Option<IcalVCalendar>,
}

impl Drop for IcalVEvent {
  fn drop(&mut self) {
    unsafe {
      // println!("free");
      ical::icalcomponent_free(self.ptr);
    }
  }
}

impl IcalComponent for IcalVEvent {
  fn get_ptr (&self) -> *mut ical::icalcomponent {
    self.ptr
  }
  fn as_component(&self) -> &dyn IcalComponent {
    self
  }
}

impl IcalVEvent {
  pub fn from_ptr_with_parent(
      ptr: *mut ical::icalcomponent,
      parent: &IcalVCalendar,
      ) -> IcalVEvent {
    IcalVEvent {
      ptr,
      parent: Some(parent.shallow_copy()),
      //instance_timestamp: None,
    }
  }

  pub fn get_dtend(&self) -> Option<IcalTime> {
    unsafe {
      let dtend = ical::icalcomponent_get_dtend(self.ptr);
      trace!("{:?}", dtend);
      if ical::icaltime_is_null_time(dtend) == 1 {
        None
      } else {
        Some(IcalTime::from(dtend))
      }
    }
  }

  pub fn get_duration(&self) -> Option<IcalDuration> {
    unsafe {
      let duration = ical::icalcomponent_get_duration(self.ptr);
      if ical::icaldurationtype_is_bad_duration(duration) == 0 {
        Some(IcalDuration::from(duration))
      } else {
        None
      }
    }
  }

  pub fn get_dtstart(&self) -> Option<IcalTime> {
    unsafe {
      let dtstart = ical::icalcomponent_get_dtstart(self.ptr);
      if ical::icaltime_is_null_time(dtstart) == 1 {
        None
      } else {
        Some(IcalTime::from(dtstart))
      }
    }
  }

  pub fn has_property_rrule(&self) -> bool {
    !self.get_properties(ical::icalproperty_kind_ICAL_RRULE_PROPERTY).is_empty()
  }

  pub fn get_recur_datetimes(&self) -> Vec<IcalTime> {
    let mut result: Vec<IcalTime> = vec!();
    let result_ptr: *mut ::std::os::raw::c_void = &mut result as *mut _ as *mut ::std::os::raw::c_void;

    let dtstart = self.get_dtstart().unwrap();
    unsafe {
      let mut dtend = ical::icalcomponent_get_dtend(self.ptr);

      //unroll up to 1 year in the future
      dtend.year += 1;

      ical::icalcomponent_foreach_recurrence(self.ptr, *dtstart, dtend, Some(recur_callback), result_ptr);
    }

    if dtstart.is_date() {
      result = result.into_iter().map(|time| time.as_date()).collect();
    }

    result
  }

  pub fn shallow_copy(&self) -> IcalVEvent {
    IcalVEvent {
      ptr: self.ptr,
      parent: self.parent.as_ref().map(|parent| parent.shallow_copy()),
    }
  }

  //TODO remove this function
  pub(in crate::icalwrap) fn with_internal_timestamp(&self, _datetime: &IcalTime) -> IcalVEvent {
    IcalVEvent {
      ptr: self.ptr,
      parent: self.parent.as_ref().map(|parent| parent.shallow_copy()),
    }
  }

  pub fn get_parent(&self) -> Option<&IcalVCalendar> {
    self.parent.as_ref()
  }

  pub fn get_summary(&self) -> Option<String> {
    unsafe {
      let ptr = ical::icalcomponent_get_summary(self.ptr);
      if !ptr.is_null() {
        Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
      } else {
        None
      }
    }
  }

  pub fn get_description(&self) -> Option<String> {
    unsafe {
      let ptr = ical::icalcomponent_get_description(self.ptr);
      if !ptr.is_null() {
        Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
      } else {
        None
      }
    }
  }

  pub fn get_location(&self) -> Option<String> {
    unsafe {
      let ptr = ical::icalcomponent_get_location(self.ptr);
      if !ptr.is_null() {
        Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
      } else {
        None
      }
    }
  }

  pub fn get_uid(&self) -> String {
    unsafe {
      let cstr = CStr::from_ptr(ical::icalcomponent_get_uid(self.ptr));
      cstr.to_string_lossy().into_owned()
    }
  }

  pub fn is_allday(&self) -> bool {
    unsafe {
      let dtstart = ical::icalcomponent_get_dtstart(self.ptr);
      dtstart.is_date == 1
    }
  }
}

extern "C" fn recur_callback(
                         _comp: *mut ical::icalcomponent,
                         span: *mut ical::icaltime_span,
                         data: *mut ::std::os::raw::c_void) {
  let data: &mut Vec<IcalTime> = unsafe { &mut *(data as *mut Vec<IcalTime>) };

  let spanstart = unsafe {
    let start = (*span).start;
    IcalTime::from_timestamp(start)
  };

  data.push(spanstart);
}

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


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

    let event = cal.get_principal_event();
    let props = event.get_properties_all();
    assert_eq!(7, props.len());
  }

  #[test]
  fn test_get_property_get_value() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, None).unwrap();
    let event = cal.get_principal_event();
    let prop = event.get_properties_by_name("DTSTART");

    assert_eq!(1, prop.len());
    assert_eq!("DTSTART", prop[0].get_name());
    assert_eq!("20070628", prop[0].get_value());
    assert_eq!(NaiveDate::from_ymd_opt(2007,6,28), prop[0].get_value_as_date());
  }

  #[test]
  fn test_get_property_debug() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_ALLDAY, None).unwrap();
    let event = cal.get_principal_event();
    let prop = event.get_property(ical::icalproperty_kind_ICAL_DTSTART_PROPERTY).unwrap();

    assert_eq!("DTSTART;VALUE=DATE:20070628", format!("{:?}", prop));
  }

  #[test]
  fn test_get_summary() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap();
    let event = cal.get_principal_event();

    assert_eq!(Some("Festival International de Jazz de Montreal".to_string()), event.get_summary());
  }

  #[test]
  fn test_get_summary_none() {
    let cal = IcalVCalendar::from_str(testdata::TEST_NO_SUMMARY, None).unwrap();
    let event = cal.get_principal_event();

    assert_eq!(None, event.get_summary());
  }

  #[test]
  fn test_get_duration() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap();
    let event = cal.get_principal_event();

    assert_eq!(Some(IcalDuration::from_seconds(10*24*60*60 + 18*60*60)), event.get_duration());
  }


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

    assert_eq!(Some("Discuss how we can test c&s interoperability\nusing iCalendar and other IETF standards.".to_string()), event.get_description());
  }

  #[test]
  fn test_get_description_none() {
    let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap();
    let event = cal.get_principal_event();

    assert_eq!(None, event.get_description());
  }

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

    assert_eq!</