12 changed files with 314 additions and 163 deletions
@ -1,17 +1,37 @@
|
||||
use my_tasks::client::Client; |
||||
use std::path::Path; |
||||
|
||||
use my_tasks::{client::Client, traits::CalDavSource}; |
||||
use my_tasks::cache::Cache; |
||||
use my_tasks::Provider; |
||||
use my_tasks::settings::URL; |
||||
use my_tasks::settings::USERNAME; |
||||
use my_tasks::settings::PASSWORD; |
||||
|
||||
|
||||
const CACHE_FILE: &str = "caldav_cache.json"; |
||||
|
||||
#[tokio::main] |
||||
async fn main() { |
||||
// This is just a function to silence "unused function" warning
|
||||
/* |
||||
let cache_path = Path::new(CACHE_FILE); |
||||
|
||||
let mut client = Client::new(URL, USERNAME, PASSWORD).unwrap(); |
||||
let calendars = client.get_calendars().await.unwrap(); |
||||
let _ = calendars.iter() |
||||
.map(|cal| println!(" {}\t{}", cal.name(), cal.url().as_str())) |
||||
.collect::<()>(); |
||||
let _ = client.get_tasks(&calendars[3].url()).await; |
||||
let mut cache = match Cache::from_file(&cache_path) { |
||||
Ok(cache) => cache, |
||||
Err(err) => { |
||||
log::warn!("Invalid cache file: {}. Using a default cache", err); |
||||
Cache::new(&cache_path) |
||||
} |
||||
}; |
||||
let provider = Provider::new(client, cache); |
||||
|
||||
let cals = provider.local().get_calendars().await.unwrap(); |
||||
println!("---- before sync -----"); |
||||
my_tasks::utils::print_calendar_list(cals); |
||||
|
||||
provider.sync(); |
||||
println!("---- after sync -----"); |
||||
my_tasks::utils::print_calendar_list(cals); |
||||
*/ |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,124 @@
|
||||
use std::convert::TryFrom; |
||||
use std::error::Error; |
||||
use std::collections::HashMap; |
||||
use std::collections::BTreeMap; |
||||
|
||||
use url::Url; |
||||
use serde::{Deserialize, Serialize}; |
||||
use chrono::{DateTime, Utc}; |
||||
|
||||
use crate::traits::{PartialCalendar, CompleteCalendar}; |
||||
use crate::calendar::{SupportedComponents, SearchFilter}; |
||||
use crate::Item; |
||||
use crate::item::ItemId; |
||||
|
||||
|
||||
/// A calendar used by the [`cache`](crate::cache) module
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
||||
pub struct CachedCalendar { |
||||
name: String, |
||||
url: Url, |
||||
supported_components: SupportedComponents, |
||||
|
||||
items: Vec<Item>, |
||||
deleted_items: BTreeMap<DateTime<Utc>, ItemId>, |
||||
} |
||||
|
||||
impl CachedCalendar { |
||||
/// Create a new calendar
|
||||
pub fn new(name: String, url: Url, supported_components: SupportedComponents) -> Self { |
||||
Self { |
||||
name, url, supported_components, |
||||
items: Vec::new(), |
||||
deleted_items: BTreeMap::new(), |
||||
} |
||||
} |
||||
|
||||
/// Returns the list of tasks that this calendar contains
|
||||
pub fn get_tasks(&self) -> HashMap<ItemId, &Item> { |
||||
self.get_tasks_modified_since(None) |
||||
} |
||||
/// Returns the tasks that have been last-modified after `since`
|
||||
pub fn get_tasks_modified_since(&self, since: Option<DateTime<Utc>>) -> HashMap<ItemId, &Item> { |
||||
self.get_items_modified_since(since, Some(SearchFilter::Tasks)) |
||||
} |
||||
} |
||||
|
||||
impl PartialCalendar for CachedCalendar { |
||||
fn name(&self) -> &str { |
||||
&self.name |
||||
} |
||||
|
||||
fn url(&self) -> &Url { |
||||
&self.url |
||||
} |
||||
|
||||
fn supported_components(&self) -> SupportedComponents { |
||||
self.supported_components |
||||
} |
||||
|
||||
fn add_item(&mut self, item: Item) { |
||||
self.items.push(item); |
||||
} |
||||
|
||||
fn delete_item(&mut self, item_id: &ItemId) { |
||||
self.items.retain(|i| i.id() != item_id); |
||||
self.deleted_items.insert(Utc::now(), item_id.clone()); |
||||
} |
||||
|
||||
fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, filter: Option<SearchFilter>) -> HashMap<ItemId, &Item> { |
||||
let filter = filter.unwrap_or_default(); |
||||
|
||||
let mut map = HashMap::new(); |
||||
|
||||
for item in &self.items { |
||||
match since { |
||||
None => (), |
||||
Some(since) => if item.last_modified() < since { |
||||
continue; |
||||
}, |
||||
} |
||||
|
||||
match filter { |
||||
SearchFilter::Tasks => { |
||||
if item.is_task() == false { |
||||
continue; |
||||
} |
||||
}, |
||||
_ => (), |
||||
} |
||||
|
||||
map.insert(item.id().clone(), item); |
||||
} |
||||
|
||||
map |
||||
} |
||||
|
||||
fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item> { |
||||
for item in &mut self.items { |
||||
if item.id() == id { |
||||
return Some(item); |
||||
} |
||||
} |
||||
return None; |
||||
} |
||||
|
||||
fn find_missing_items_compared_to(&self, other: &dyn PartialCalendar) -> Vec<ItemId> { |
||||
unimplemented!("todo"); |
||||
} |
||||
} |
||||
|
||||
impl CompleteCalendar for CachedCalendar { |
||||
/// Returns the items that have been deleted after `since`
|
||||
fn get_items_deleted_since(&self, since: DateTime<Utc>) -> Vec<ItemId> { |
||||
self.deleted_items.range(since..) |
||||
.map(|(_key, value)| value.clone()) |
||||
.collect() |
||||
} |
||||
|
||||
/// Returns the list of items that this calendar contains
|
||||
fn get_items(&self) -> HashMap<ItemId, &Item> { |
||||
self.get_items_modified_since(None, None) |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,38 @@
|
||||
use crate::traits::PartialCalendar; |
||||
|
||||
/// A CalDAV calendar created by a [`Client`](crate::client::Client).
|
||||
pub struct RemoteCalendar { |
||||
name: String, |
||||
url: Url, |
||||
supported_components: SupportedComponents |
||||
} |
||||
|
||||
impl PartialCalendar for RemoteCalendar { |
||||
fn name(&self) -> &str { |
||||
&self.name |
||||
} |
||||
|
||||
fn supported_components(&self) -> crate::calendar::SupportedComponents { |
||||
self.supported_components |
||||
} |
||||
|
||||
fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, filter: Option<crate::calendar::SearchFilter>) |
||||
-> HashMap<ItemId, &Item> |
||||
{ |
||||
log::error!("Not implemented"); |
||||
HashMap::new() |
||||
} |
||||
|
||||
fn get_item_by_id_mut(&mut self, id: &ItemId) -> Option<&mut Item> { |
||||
log::error!("Not implemented"); |
||||
None |
||||
} |
||||
|
||||
fn add_item(&mut self, item: Item) { |
||||
log::error!("Not implemented"); |
||||
} |
||||
|
||||
fn delete_item(&mut self, item_id: &ItemId) { |
||||
log::error!("Not implemented"); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue