diff --git a/src/calendar/mod.rs b/src/calendar/mod.rs index 0585466..e0c43b7 100644 --- a/src/calendar/mod.rs +++ b/src/calendar/mod.rs @@ -18,6 +18,19 @@ bitflags! { } } +impl SupportedComponents { + pub fn to_xml_string(&self) -> String { + format!(r#" + + {} {} + + "#, + if self.contains(Self::EVENT) { "" } else { "" }, + if self.contains(Self::TODO) { "" } else { "" }, + ) + } +} + impl TryFrom for SupportedComponents { type Error = Box; diff --git a/src/client.rs b/src/client.rs index 8e1a1ce..c03fb18 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use async_trait::async_trait; +use reqwest::Method; use reqwest::header::CONTENT_TYPE; use minidom::Element; use url::Url; @@ -233,8 +234,36 @@ impl CalDavSource for Client { } async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents) -> Result>, Box> { - todo!(); + let creation_body = calendar_body(name, supported_components); + + reqwest::Client::new() + .request(Method::from_bytes(b"MKCALENDAR").unwrap(), id.clone()) + .header(CONTENT_TYPE, "application/xml") + .basic_auth(self.resource.username(), Some(self.resource.password())) + .body(creation_body) + .send() + .await?; + + self.populate_calendars().await?; + self.get_calendar(&id).await.ok_or(format!("Unable to insert calendar {:?}", id).into()) + } } +fn calendar_body(name: String, supported_components: SupportedComponents) -> String { + // This is taken from https://tools.ietf.org/html/rfc4791#page-24 + format!(r#" + + + + {} + {} + + + + "#, + name, + supported_components.to_xml_string(), + ) } diff --git a/src/settings.rs b/src/settings.rs index 1c113d0..eb0434c 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -6,6 +6,7 @@ pub const PASSWORD: &str = "secret_password"; pub const EXAMPLE_TASK_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/6121A0BE-C2E0-4F16-A3FA-658E54E7062A/74439558-CDFF-426C-92CD-ECDDACE971B0.ics"; pub const EXAMPLE_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/"; +pub const EXAMPLE_CREATED_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_that_we_have_created/"; pub const ORG_NAME: &str = "My organisation"; pub const PRODUCT_NAME: &str = "My CalDAV client";