diff --git a/src/calendar/cached_calendar.rs b/src/calendar/cached_calendar.rs index b94a00c..1c107e6 100644 --- a/src/calendar/cached_calendar.rs +++ b/src/calendar/cached_calendar.rs @@ -92,6 +92,12 @@ impl PartialCalendar for CachedCalendar { map } + fn get_item_ids(&mut self) -> Vec { + self.items.iter() + .map(|item| item.id().clone()) + .collect() + } + fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item> { for item in &mut self.items { if item.id() == id { @@ -100,10 +106,6 @@ impl PartialCalendar for CachedCalendar { } return None; } - - fn find_missing_items_compared_to(&self, _other: &dyn PartialCalendar) -> Vec { - unimplemented!("todo"); - } } impl CompleteCalendar for CachedCalendar { diff --git a/src/provider.rs b/src/provider.rs index 31c8de5..c09e4c2 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -72,9 +72,8 @@ where Some(cal) => cal, }; - let server_mod = cal_server.get_items_modified_since(last_sync, None); let server_del = match last_sync { - Some(_date) => cal_server.find_missing_items_compared_to(cal_local), + Some(_date) => cal_server.find_deletions(cal_local.get_item_ids()), None => Vec::new(), }; let local_del = match last_sync { @@ -88,6 +87,7 @@ where for deleted_id in server_del { tasks_id_to_remove_from_local.push(deleted_id); } + let server_mod = cal_server.get_items_modified_since(last_sync, None); for (new_id, new_item) in &server_mod { if server_mod.contains_key(new_id) { log::warn!("Conflict for task {} ({}). Using the server version.", new_item.name(), new_id); diff --git a/src/traits.rs b/src/traits.rs index 9ffbbae..28a48bb 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -54,6 +54,9 @@ pub trait PartialCalendar { fn get_items_modified_since(&self, since: Option>, filter: Option) -> HashMap; + /// Get the IDs of all current items in this calendar + fn get_item_ids(&mut self) -> Vec; + /// Returns a particular item fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item>; @@ -63,9 +66,29 @@ pub trait PartialCalendar { /// Remove an item from this calendar fn delete_item(&mut self, item_id: &ItemId); - /// Compares with another calendar and lists missing items - /// This function is a sort of replacement for `get_items_deleted_since`, that is not available on PartialCalendars - fn find_missing_items_compared_to(&self, other: &dyn PartialCalendar) -> Vec; + + /// Returns whether this calDAV calendar supports to-do items + fn supports_todo(&self) -> bool { + self.supported_components().contains(crate::calendar::SupportedComponents::TODO) + } + + /// Returns whether this calDAV calendar supports calendar items + fn supports_events(&self) -> bool { + self.supported_components().contains(crate::calendar::SupportedComponents::EVENT) + } + + /// Finds the IDs of the items that are missing compared to a reference set + fn find_deletions(&mut self, reference_set: Vec) -> Vec { + let mut deletions = Vec::new(); + + let current_items = self.get_item_ids(); + for original_item in reference_set { + if current_items.contains(&original_item) == false { + deletions.push(original_item); + } + } + deletions + } } /// A calendar we always know everything about. @@ -81,16 +104,3 @@ pub trait CompleteCalendar : PartialCalendar { fn get_items(&self) -> HashMap; } - - -impl PartialCalendar { - /// Returns whether this calDAV calendar supports to-do items - pub fn supports_todo(&self) -> bool { - self.supported_components().contains(crate::calendar::SupportedComponents::TODO) - } - - /// Returns whether this calDAV calendar supports calendar items - pub fn supports_events(&self) -> bool { - self.supported_components().contains(crate::calendar::SupportedComponents::EVENT) - } -}