|
| 1 | +use chrono::{DateTime, Utc}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::future::Future; |
| 4 | + |
| 5 | +pub use anyhow::Error; |
| 6 | + |
| 7 | +pub trait CalendarSource { |
| 8 | + fn list_calendars(&self) -> impl Future<Output = Result<Vec<Calendar>, Error>>; |
| 9 | + fn list_events(&self, filter: EventFilter) -> impl Future<Output = Result<Vec<Event>, Error>>; |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 13 | +pub enum Platform { |
| 14 | + Apple, |
| 15 | + Google, |
| 16 | + Outlook, |
| 17 | +} |
| 18 | + |
| 19 | +impl std::fmt::Display for Platform { |
| 20 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 21 | + match self { |
| 22 | + Platform::Apple => write!(f, "Apple"), |
| 23 | + Platform::Google => write!(f, "Google"), |
| 24 | + Platform::Outlook => write!(f, "Outlook"), |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 30 | +pub struct Calendar { |
| 31 | + pub id: String, |
| 32 | + pub platform: Platform, |
| 33 | + pub name: String, |
| 34 | + pub source: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 38 | +pub struct Event { |
| 39 | + pub id: String, |
| 40 | + pub calendar_id: String, |
| 41 | + pub platform: Platform, |
| 42 | + pub name: String, |
| 43 | + pub note: String, |
| 44 | + pub participants: Vec<Participant>, |
| 45 | + pub start_date: DateTime<Utc>, |
| 46 | + pub end_date: DateTime<Utc>, |
| 47 | + pub google_event_url: Option<String>, |
| 48 | + #[serde(default)] |
| 49 | + pub is_recurring: bool, |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 53 | +pub struct Participant { |
| 54 | + pub name: String, |
| 55 | + pub email: Option<String>, |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(Debug, Serialize, Deserialize)] |
| 59 | +pub struct EventFilter { |
| 60 | + pub from: DateTime<Utc>, |
| 61 | + pub to: DateTime<Utc>, |
| 62 | + pub calendar_tracking_id: String, |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Debug, Serialize, Deserialize)] |
| 66 | +pub enum Opener { |
| 67 | + AppleScript(String), |
| 68 | + Url(String), |
| 69 | +} |
| 70 | + |
| 71 | +impl Event { |
| 72 | + pub fn opener(&self) -> anyhow::Result<Opener> { |
| 73 | + match self.platform { |
| 74 | + Platform::Apple => { |
| 75 | + let script = String::from( |
| 76 | + " |
| 77 | + tell application \"Calendar\" |
| 78 | + activate |
| 79 | + switch view to month view |
| 80 | + view calendar at current date |
| 81 | + end tell |
| 82 | + ", |
| 83 | + ); |
| 84 | + |
| 85 | + Ok(Opener::AppleScript(script)) |
| 86 | + } |
| 87 | + Platform::Google => { |
| 88 | + let url = self.google_event_url.as_ref().unwrap().clone(); |
| 89 | + Ok(Opener::Url(url)) |
| 90 | + } |
| 91 | + Platform::Outlook => { |
| 92 | + anyhow::bail!("Outlook is not supported yet"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(target_os = "macos")] |
1 | 99 | use itertools::Itertools; |
| 100 | +#[cfg(target_os = "macos")] |
2 | 101 | use std::time::Duration; |
3 | 102 |
|
| 103 | +#[cfg(target_os = "macos")] |
4 | 104 | use objc2::msg_send; |
5 | 105 |
|
| 106 | +#[cfg(target_os = "macos")] |
6 | 107 | use block2::RcBlock; |
| 108 | +#[cfg(target_os = "macos")] |
7 | 109 | use objc2::{rc::Retained, runtime::Bool, ClassType}; |
| 110 | +#[cfg(target_os = "macos")] |
8 | 111 | use objc2_contacts::{CNAuthorizationStatus, CNContactStore, CNEntityType}; |
| 112 | +#[cfg(target_os = "macos")] |
9 | 113 | use objc2_event_kit::{ |
10 | 114 | EKAuthorizationStatus, EKCalendar, EKEntityType, EKEvent, EKEventStore, EKParticipant, |
11 | 115 | }; |
| 116 | +#[cfg(target_os = "macos")] |
12 | 117 | use objc2_foundation::{NSArray, NSDate, NSError, NSString}; |
13 | 118 |
|
14 | | -use hypr_calendar_interface::{ |
15 | | - Calendar, CalendarSource, Error, Event, EventFilter, Participant, Platform, |
16 | | -}; |
17 | | - |
| 119 | +#[cfg(target_os = "macos")] |
18 | 120 | pub struct Handle { |
19 | 121 | event_store: Retained<EKEventStore>, |
20 | 122 | contacts_store: Retained<CNContactStore>, |
21 | 123 | calendar_access_granted: bool, |
22 | 124 | contacts_access_granted: bool, |
23 | 125 | } |
24 | 126 |
|
| 127 | +#[cfg(target_os = "macos")] |
25 | 128 | #[allow(clippy::new_without_default)] |
26 | 129 | impl Handle { |
27 | 130 | pub fn new() -> Self { |
@@ -166,6 +269,7 @@ impl Handle { |
166 | 269 | } |
167 | 270 | } |
168 | 271 |
|
| 272 | +#[cfg(target_os = "macos")] |
169 | 273 | impl CalendarSource for Handle { |
170 | 274 | async fn list_calendars(&self) -> Result<Vec<Calendar>, Error> { |
171 | 275 | if !self.calendar_access_granted { |
@@ -263,6 +367,7 @@ impl CalendarSource for Handle { |
263 | 367 | } |
264 | 368 | } |
265 | 369 |
|
| 370 | +#[cfg(target_os = "macos")] |
266 | 371 | fn offset_date_time_from(date: Retained<NSDate>) -> chrono::DateTime<chrono::Utc> { |
267 | 372 | let seconds = unsafe { date.timeIntervalSinceReferenceDate() }; |
268 | 373 |
|
|
0 commit comments