2021-11-10 22:47:33 +01:00
//! This is an example of how kitchen-fridge can be used
2021-03-01 23:39:16 +01:00
use std ::path ::Path ;
2021-04-17 19:31:09 +02:00
use chrono ::{ Utc } ;
2021-11-15 23:52:26 +01:00
use url ::Url ;
2021-04-17 19:31:09 +02:00
2021-04-28 23:46:11 +02:00
use kitchen_fridge ::{ client ::Client , traits ::CalDavSource } ;
2021-11-16 00:10:47 +01:00
use kitchen_fridge ::calendar ::SupportedComponents ;
2021-04-28 23:46:11 +02:00
use kitchen_fridge ::Item ;
use kitchen_fridge ::Task ;
use kitchen_fridge ::task ::CompletionStatus ;
use kitchen_fridge ::cache ::Cache ;
use kitchen_fridge ::CalDavProvider ;
use kitchen_fridge ::traits ::BaseCalendar ;
use kitchen_fridge ::traits ::CompleteCalendar ;
use kitchen_fridge ::utils ::pause ;
2021-02-24 23:49:20 +01:00
2021-04-19 08:30:34 +02:00
const CACHE_FOLDER : & str = " test_cache/provider_sync " ;
2021-03-31 08:32:28 +02:00
2021-03-01 23:39:16 +01:00
2021-11-10 22:47:33 +01:00
// TODO: change these values with yours
pub const URL : & str = " https://my.server.com/remote.php/dav/files/john " ;
pub const USERNAME : & str = " username " ;
pub const PASSWORD : & str = " secret_password " ;
pub const EXAMPLE_EXISTING_CALENDAR_URL : & str = " https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/ " ;
2021-11-15 22:21:55 +01:00
pub const EXAMPLE_CREATED_CALENDAR_URL : & str = " https://my.server.com/remote.php/dav/calendars/john/a_calendar_that_we_have_created/ " ;
2021-11-10 22:47:33 +01:00
2021-02-24 23:49:20 +01:00
#[ tokio::main ]
async fn main ( ) {
2021-03-31 08:32:28 +02:00
env_logger ::init ( ) ;
2021-04-13 23:30:43 +02:00
println! ( " This examples show how to sync a remote server with a local cache, using a Provider. " ) ;
2021-11-15 22:32:44 +01:00
println! ( " Make sure you have edited the constants in this file to include correct URLs and credentials. " ) ;
2021-04-13 23:30:43 +02:00
println! ( " You can also set the RUST_LOG environment variable to display more info about the sync. " ) ;
2021-11-15 22:32:44 +01:00
println! ( " " ) ;
println! ( " This will use the following settings: " ) ;
println! ( " * URL = {} " , URL ) ;
println! ( " * USERNAME = {} " , USERNAME ) ;
println! ( " * EXAMPLE_EXISTING_CALENDAR_URL = {} " , EXAMPLE_EXISTING_CALENDAR_URL ) ;
println! ( " * EXAMPLE_CREATED_CALENDAR_URL = {} " , EXAMPLE_CREATED_CALENDAR_URL ) ;
2021-04-13 23:30:43 +02:00
pause ( ) ;
2021-04-11 19:15:04 +02:00
2021-03-31 08:32:28 +02:00
let cache_path = Path ::new ( CACHE_FOLDER ) ;
2021-02-24 23:49:20 +01:00
2021-03-31 08:32:28 +02:00
let client = Client ::new ( URL , USERNAME , PASSWORD ) . unwrap ( ) ;
let cache = match Cache ::from_folder ( & cache_path ) {
2021-03-01 23:39:16 +01:00
Ok ( cache ) = > cache ,
Err ( err ) = > {
log ::warn! ( " Invalid cache file: {}. Using a default cache " , err ) ;
Cache ::new ( & cache_path )
}
} ;
2021-04-28 23:43:03 +02:00
let mut provider = CalDavProvider ::new ( client , cache ) ;
2021-03-01 23:39:16 +01:00
let cals = provider . local ( ) . get_calendars ( ) . await . unwrap ( ) ;
2021-04-17 19:31:09 +02:00
println! ( " ---- Local items, before sync ----- " ) ;
2021-04-28 23:46:11 +02:00
kitchen_fridge ::utils ::print_calendar_list ( & cals ) . await ;
2021-03-01 23:39:16 +01:00
2021-04-13 23:30:43 +02:00
println! ( " Starting a sync... " ) ;
2021-11-23 23:26:48 +01:00
println! ( " Depending on your RUST_LOG value, you may see more or less details about the progress. " ) ;
// Note that we could use sync_with_feedback() to have better and formatted feedback
2021-04-11 19:15:04 +02:00
if provider . sync ( ) . await = = false {
log ::warn! ( " Sync did not complete, see the previous log lines for more info. You can safely start a new sync. " ) ;
2021-03-31 08:32:28 +02:00
}
2021-04-13 23:30:43 +02:00
provider . local ( ) . save_to_folder ( ) . unwrap ( ) ;
2021-04-11 19:15:04 +02:00
2021-04-17 19:31:09 +02:00
println! ( " ---- Local items, after sync ----- " ) ;
2021-03-31 08:32:28 +02:00
let cals = provider . local ( ) . get_calendars ( ) . await . unwrap ( ) ;
2021-04-28 23:46:11 +02:00
kitchen_fridge ::utils ::print_calendar_list ( & cals ) . await ;
2021-04-13 23:30:43 +02:00
2021-04-20 00:01:05 +02:00
add_items_and_sync_again ( & mut provider ) . await ;
2021-04-13 23:30:43 +02:00
}
2021-12-06 23:07:38 +01:00
async fn add_items_and_sync_again ( provider : & mut CalDavProvider ) {
2021-04-20 00:01:05 +02:00
println! ( " \n Now, we'll add a calendar and a few tasks and run the sync again. " ) ;
2021-04-13 23:30:43 +02:00
pause ( ) ;
2021-04-20 00:01:05 +02:00
// Create a new calendar...
2021-11-16 00:10:47 +01:00
let new_calendar_url : Url = EXAMPLE_CREATED_CALENDAR_URL . parse ( ) . unwrap ( ) ;
2021-04-20 00:01:05 +02:00
let new_calendar_name = " A brave new calendar " . to_string ( ) ;
2021-04-20 00:00:18 +02:00
if let Err ( _err ) = provider . local_mut ( )
2021-11-23 23:31:51 +01:00
. create_calendar ( new_calendar_url . clone ( ) , new_calendar_name . clone ( ) , SupportedComponents ::TODO , Some ( " #ff8000 " . parse ( ) . unwrap ( ) ) )
2021-04-20 00:00:18 +02:00
. await {
println! ( " Unable to add calendar, maybe it exists already. We're not adding it after all. " ) ;
}
2021-04-20 00:01:05 +02:00
// ...and add a task in it
let new_name = " This is a new task in a new calendar " ;
2021-11-16 00:10:47 +01:00
let new_task = Task ::new ( String ::from ( new_name ) , true , & new_calendar_url ) ;
provider . local ( ) . get_calendar ( & new_calendar_url ) . await . unwrap ( )
2021-04-20 00:01:05 +02:00
. lock ( ) . unwrap ( ) . add_item ( Item ::Task ( new_task ) ) . await . unwrap ( ) ;
2021-04-17 19:31:09 +02:00
2021-04-20 00:01:05 +02:00
// Also create a task in a previously existing calendar
2021-11-16 00:10:47 +01:00
let changed_calendar_url : Url = EXAMPLE_EXISTING_CALENDAR_URL . parse ( ) . unwrap ( ) ;
2021-04-20 00:01:05 +02:00
let new_task_name = " This is a new task we're adding as an example, with ÜTF-8 characters " ;
2021-11-16 00:10:47 +01:00
let new_task = Task ::new ( String ::from ( new_task_name ) , false , & changed_calendar_url ) ;
2021-11-15 23:52:26 +01:00
let new_url = new_task . url ( ) . clone ( ) ;
2021-11-16 00:10:47 +01:00
provider . local ( ) . get_calendar ( & changed_calendar_url ) . await . unwrap ( )
2021-04-17 19:31:09 +02:00
. lock ( ) . unwrap ( ) . add_item ( Item ::Task ( new_task ) ) . await . unwrap ( ) ;
2021-04-13 23:30:43 +02:00
2021-04-20 00:01:05 +02:00
2021-04-13 23:30:43 +02:00
if provider . sync ( ) . await = = false {
log ::warn! ( " Sync did not complete, see the previous log lines for more info. You can safely start a new sync. The new task may not have been synced. " ) ;
} else {
2021-04-20 00:01:05 +02:00
println! ( " Done syncing the new task ' {} ' and the new calendar ' {} ' " , new_task_name , new_calendar_name ) ;
2021-04-13 23:30:43 +02:00
}
provider . local ( ) . save_to_folder ( ) . unwrap ( ) ;
2021-11-16 00:10:47 +01:00
complete_item_and_sync_again ( provider , & changed_calendar_url , & new_url ) . await ;
2021-04-17 19:31:09 +02:00
}
async fn complete_item_and_sync_again (
2021-04-28 23:43:03 +02:00
provider : & mut CalDavProvider ,
2021-11-16 00:10:47 +01:00
changed_calendar_url : & Url ,
2021-11-15 23:52:26 +01:00
url_to_complete : & Url )
2021-04-17 19:31:09 +02:00
{
println! ( " \n Now, we'll mark this last task as completed, and run the sync again. " ) ;
pause ( ) ;
let completion_status = CompletionStatus ::Completed ( Some ( Utc ::now ( ) ) ) ;
2021-11-16 00:10:47 +01:00
provider . local ( ) . get_calendar ( changed_calendar_url ) . await . unwrap ( )
. lock ( ) . unwrap ( ) . get_item_by_url_mut ( url_to_complete ) . await . unwrap ( )
2021-04-17 19:31:09 +02:00
. unwrap_task_mut ( )
. set_completion_status ( completion_status ) ;
if provider . sync ( ) . await = = false {
log ::warn! ( " Sync did not complete, see the previous log lines for more info. You can safely start a new sync. The new task may not have been synced. " ) ;
} else {
println! ( " Done syncing the completed task " ) ;
}
provider . local ( ) . save_to_folder ( ) . unwrap ( ) ;
2021-11-16 00:10:47 +01:00
remove_items_and_sync_again ( provider , changed_calendar_url , url_to_complete ) . await ;
2021-04-17 19:31:09 +02:00
}
2021-04-20 00:01:05 +02:00
async fn remove_items_and_sync_again (
2021-04-28 23:43:03 +02:00
provider : & mut CalDavProvider ,
2021-11-16 00:10:47 +01:00
changed_calendar_url : & Url ,
2021-11-15 23:52:26 +01:00
id_to_remove : & Url )
2021-04-17 19:31:09 +02:00
{
println! ( " \n Now, we'll delete this last task, and run the sync again. " ) ;
pause ( ) ;
2021-04-20 00:01:05 +02:00
// Remove the task we had created
2021-11-16 00:10:47 +01:00
provider . local ( ) . get_calendar ( changed_calendar_url ) . await . unwrap ( )
2021-04-17 19:31:09 +02:00
. lock ( ) . unwrap ( )
. mark_for_deletion ( id_to_remove ) . await . unwrap ( ) ;
if provider . sync ( ) . await = = false {
log ::warn! ( " Sync did not complete, see the previous log lines for more info. You can safely start a new sync. The new task may not have been synced. " ) ;
} else {
println! ( " Done syncing the deleted task " ) ;
}
provider . local ( ) . save_to_folder ( ) . unwrap ( ) ;
2021-04-13 23:30:43 +02:00
println! ( " Done. You can start this example again to see the cache being restored from its current saved state " )
2021-02-24 23:49:20 +01:00
}