| 1 | use app_base::ui_locale::{tr, tr_args}; |
| 2 | use chrono::Utc; |
| 3 | use gpui_kit::Context; |
| 4 | use store::{ |
| 5 | CalendarAccounts, CalendarRef, GoogleAccount, GoogleCalendarInfo, GoogleTokenSet, |
| 6 | load_calendar_accounts, save_calendar_accounts, |
| 7 | }; |
| 8 | use uuid::Uuid; |
| 9 | |
| 10 | use chatsvc::{ |
| 11 | GoogleCalendar, GoogleOAuth, allocate_redirect_uri, exchange_code, parse_calendar_list, |
| 12 | wait_for_code, |
| 13 | }; |
| 14 | |
| 15 | use crate::app_state::AppState; |
| 16 | use crate::runtime; |
| 17 | |
| 18 | impl AppState { |
| 19 | pub fn calendar_accounts(&self) -> CalendarAccounts { |
| 20 | load_calendar_accounts(&self.store) |
| 21 | } |
| 22 | |
| 23 | pub fn google_connect_pending(&self) -> Option<&str> { |
| 24 | self.google_connect_pending.as_deref() |
| 25 | } |
| 26 | |
| 27 | pub fn disconnect_google_account(&mut self, account_id: &str, cx: &mut Context<Self>) { |
| 28 | let mut accounts = load_calendar_accounts(&self.store); |
| 29 | accounts.disconnect_google(account_id); |
| 30 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 31 | self.refresh_calendar(cx); |
| 32 | self.load_calendar_week(cx); |
| 33 | cx.notify(); |
| 34 | } |
| 35 | |
| 36 | pub fn connect_caldav_account( |
| 37 | &mut self, |
| 38 | url: String, |
| 39 | username: String, |
| 40 | password: String, |
| 41 | cx: &mut Context<Self>, |
| 42 | ) { |
| 43 | let store = self.store.clone(); |
| 44 | let connecting = runtime::spawn(async move { |
| 45 | let mut accounts = load_calendar_accounts(&store); |
| 46 | let id = crate::caldav_sync::connect_caldav(&mut accounts, &url, &username, &password) |
| 47 | .await?; |
| 48 | save_calendar_accounts(&store, &accounts).map_err(|error| error.to_string())?; |
| 49 | Ok::<_, String>(id) |
| 50 | }); |
| 51 | cx.spawn(async move |this, cx| match connecting.await { |
| 52 | Ok(Ok(_)) => { |
| 53 | this.update(cx, |state, cx| { |
| 54 | state.raise_notice(tr("settings.calendar.caldav_connected"), None, cx); |
| 55 | state.refresh_calendar(cx); |
| 56 | state.load_calendar_week(cx); |
| 57 | cx.notify(); |
| 58 | }) |
| 59 | .ok(); |
| 60 | } |
| 61 | Ok(Err(error)) => { |
| 62 | this.update(cx, |state, cx| { |
| 63 | state.raise_notice( |
| 64 | crate::caldav_credentials::redact_secret_text(&error), |
| 65 | None, |
| 66 | cx, |
| 67 | ); |
| 68 | }) |
| 69 | .ok(); |
| 70 | } |
| 71 | Err(_) => { |
| 72 | this.update(cx, |state, cx| { |
| 73 | state.raise_notice(tr("settings.calendar.caldav_connect_failed"), None, cx); |
| 74 | }) |
| 75 | .ok(); |
| 76 | } |
| 77 | }) |
| 78 | .detach(); |
| 79 | } |
| 80 | |
| 81 | pub fn disconnect_caldav_account(&mut self, account_id: &str, cx: &mut Context<Self>) { |
| 82 | let mut accounts = load_calendar_accounts(&self.store); |
| 83 | if let Err(error) = crate::caldav_sync::disconnect_caldav(&mut accounts, account_id) { |
| 84 | self.raise_notice(error, None, cx); |
| 85 | return; |
| 86 | } |
| 87 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 88 | self.refresh_calendar(cx); |
| 89 | self.load_calendar_week(cx); |
| 90 | cx.notify(); |
| 91 | } |
| 92 | |
| 93 | pub fn toggle_caldav_calendar_selected( |
| 94 | &mut self, |
| 95 | account_id: &str, |
| 96 | calendar_id: &str, |
| 97 | cx: &mut Context<Self>, |
| 98 | ) { |
| 99 | let mut accounts = load_calendar_accounts(&self.store); |
| 100 | if let Some(calendar) = accounts.caldav_account_mut(account_id).and_then(|account| { |
| 101 | account |
| 102 | .calendars |
| 103 | .iter_mut() |
| 104 | .find(|calendar| calendar.id == calendar_id) |
| 105 | }) { |
| 106 | calendar.selected = !calendar.selected; |
| 107 | } |
| 108 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 109 | self.refresh_calendar(cx); |
| 110 | self.load_calendar_week(cx); |
| 111 | cx.notify(); |
| 112 | } |
| 113 | |
| 114 | pub fn connect_ics_subscription( |
| 115 | &mut self, |
| 116 | url: String, |
| 117 | summary: String, |
| 118 | cx: &mut Context<Self>, |
| 119 | ) { |
| 120 | let store = self.store.clone(); |
| 121 | let connecting = runtime::spawn(async move { |
| 122 | let mut accounts = load_calendar_accounts(&store); |
| 123 | let id = crate::ics_sync::add_ics_subscription(&mut accounts, &url, &summary).await?; |
| 124 | save_calendar_accounts(&store, &accounts).map_err(|error| error.to_string())?; |
| 125 | Ok::<_, String>(id) |
| 126 | }); |
| 127 | cx.spawn(async move |this, cx| match connecting.await { |
| 128 | Ok(Ok(_)) => { |
| 129 | this.update(cx, |state, cx| { |
| 130 | state.raise_notice(tr("settings.calendar.ics_connected"), None, cx); |
| 131 | state.refresh_calendar(cx); |
| 132 | state.load_calendar_week(cx); |
| 133 | cx.notify(); |
| 134 | }) |
| 135 | .ok(); |
| 136 | } |
| 137 | Ok(Err(error)) => { |
| 138 | this.update(cx, |state, cx| state.raise_notice(error, None, cx)) |
| 139 | .ok(); |
| 140 | } |
| 141 | Err(_) => { |
| 142 | this.update(cx, |state, cx| { |
| 143 | state.raise_notice(tr("settings.calendar.ics_connect_failed"), None, cx); |
| 144 | }) |
| 145 | .ok(); |
| 146 | } |
| 147 | }) |
| 148 | .detach(); |
| 149 | } |
| 150 | |
| 151 | pub fn disconnect_ics_subscription(&mut self, subscription_id: &str, cx: &mut Context<Self>) { |
| 152 | let mut accounts = load_calendar_accounts(&self.store); |
| 153 | accounts.disconnect_ics(subscription_id); |
| 154 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 155 | self.refresh_calendar(cx); |
| 156 | self.load_calendar_week(cx); |
| 157 | cx.notify(); |
| 158 | } |
| 159 | |
| 160 | pub fn toggle_ics_subscription_selected( |
| 161 | &mut self, |
| 162 | subscription_id: &str, |
| 163 | cx: &mut Context<Self>, |
| 164 | ) { |
| 165 | let mut accounts = load_calendar_accounts(&self.store); |
| 166 | if let Some(subscription) = accounts |
| 167 | .ics_subscriptions |
| 168 | .iter_mut() |
| 169 | .find(|subscription| subscription.id == subscription_id) |
| 170 | { |
| 171 | subscription.selected = !subscription.selected; |
| 172 | } |
| 173 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 174 | self.refresh_calendar(cx); |
| 175 | self.load_calendar_week(cx); |
| 176 | cx.notify(); |
| 177 | } |
| 178 | |
| 179 | pub fn set_home_calendar(&mut self, home: CalendarRef, cx: &mut Context<Self>) { |
| 180 | let mut accounts = load_calendar_accounts(&self.store); |
| 181 | accounts.home = home; |
| 182 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 183 | cx.notify(); |
| 184 | } |
| 185 | |
| 186 | pub fn toggle_write_target(&mut self, target: CalendarRef, cx: &mut Context<Self>) { |
| 187 | let mut accounts = load_calendar_accounts(&self.store); |
| 188 | if let Some(index) = accounts |
| 189 | .write_targets |
| 190 | .iter() |
| 191 | .position(|existing| existing.key() == target.key()) |
| 192 | { |
| 193 | accounts.write_targets.remove(index); |
| 194 | } else if accounts.home.key() != target.key() { |
| 195 | accounts.write_targets.push(target); |
| 196 | } |
| 197 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 198 | cx.notify(); |
| 199 | } |
| 200 | |
| 201 | pub fn toggle_google_calendar_selected( |
| 202 | &mut self, |
| 203 | account_id: &str, |
| 204 | calendar_id: &str, |
| 205 | cx: &mut Context<Self>, |
| 206 | ) { |
| 207 | let mut accounts = load_calendar_accounts(&self.store); |
| 208 | if let Some(calendar) = accounts.google_account_mut(account_id).and_then(|account| { |
| 209 | account |
| 210 | .calendars |
| 211 | .iter_mut() |
| 212 | .find(|calendar| calendar.id == calendar_id) |
| 213 | }) { |
| 214 | calendar.selected = !calendar.selected; |
| 215 | } |
| 216 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 217 | self.refresh_calendar(cx); |
| 218 | self.load_calendar_week(cx); |
| 219 | cx.notify(); |
| 220 | } |
| 221 | |
| 222 | /// Group check box: turn every calendar of one Google account on or off. |
| 223 | pub fn set_google_account_selected( |
| 224 | &mut self, |
| 225 | account_id: &str, |
| 226 | on: bool, |
| 227 | cx: &mut Context<Self>, |
| 228 | ) { |
| 229 | let mut accounts = load_calendar_accounts(&self.store); |
| 230 | if let Some(account) = accounts.google_account_mut(account_id) { |
| 231 | for calendar in &mut account.calendars { |
| 232 | calendar.selected = on; |
| 233 | } |
| 234 | } |
| 235 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 236 | self.refresh_calendar(cx); |
| 237 | cx.notify(); |
| 238 | } |
| 239 | |
| 240 | pub fn toggle_outlook_calendar_selected(&mut self, cx: &mut Context<Self>) { |
| 241 | let mut accounts = load_calendar_accounts(&self.store); |
| 242 | accounts.outlook_selected = !accounts.outlook_selected; |
| 243 | let _ = save_calendar_accounts(&self.store, &accounts); |
| 244 | self.refresh_calendar(cx); |
| 245 | self.load_calendar_week(cx); |
| 246 | cx.notify(); |
| 247 | } |
| 248 | |
| 249 | pub fn reopen_google_connect(&mut self, cx: &mut Context<Self>) { |
| 250 | if let Some(url) = self.google_connect_pending.clone() { |
| 251 | crate::external_browser::open_url(cx, &self.store, &url); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | pub fn cancel_google_connect(&mut self, cx: &mut Context<Self>) { |
| 256 | self.google_connect_generation = self.google_connect_generation.wrapping_add(1); |
| 257 | self.google_connect_pending = None; |
| 258 | self.raise_notice(tr("settings.calendar.sign_in_cancelled"), None, cx); |
| 259 | cx.notify(); |
| 260 | } |
| 261 | |
| 262 | pub fn connect_google_calendar(&mut self, cx: &mut Context<Self>) { |
| 263 | if self.google_connect_pending.is_some() { |
| 264 | self.reopen_google_connect(cx); |
| 265 | return; |
| 266 | } |
| 267 | let store = self.store.clone(); |
| 268 | self.google_connect_generation = self.google_connect_generation.wrapping_add(1); |
| 269 | let generation = self.google_connect_generation; |
| 270 | cx.spawn(async move |this, cx| { |
| 271 | let prepared = runtime::spawn({ |
| 272 | let store = store.clone(); |
| 273 | async move { |
| 274 | let oauth = crate::google_oauth_client::resolve(&store) |
| 275 | .await |
| 276 | .ok_or_else(|| tr("settings.calendar.not_in_build"))?; |
| 277 | let (redirect_uri, listener) = allocate_redirect_uri() |
| 278 | .await |
| 279 | .map_err(|error| error.to_string())?; |
| 280 | let pending = oauth.begin(&redirect_uri); |
| 281 | let authorize_url = pending.authorize_url.clone(); |
| 282 | Ok::<_, String>((authorize_url, pending, listener, oauth)) |
| 283 | } |
| 284 | }); |
| 285 | let (authorize_url, pending, listener, oauth) = match prepared.await { |
| 286 | Ok(Ok(value)) => value, |
| 287 | Ok(Err(error)) => { |
| 288 | this.update(cx, |state, cx| { |
| 289 | clear_google_connect_if(state, generation); |
| 290 | state.raise_notice(error, None, cx); |
| 291 | }) |
| 292 | .ok(); |
| 293 | return; |
| 294 | } |
| 295 | Err(_) => { |
| 296 | this.update(cx, |state, cx| { |
| 297 | clear_google_connect_if(state, generation); |
| 298 | state.raise_notice(tr("settings.calendar.sign_in_start_failed"), None, cx); |
| 299 | }) |
| 300 | .ok(); |
| 301 | return; |
| 302 | } |
| 303 | }; |
| 304 | let still_current = this |
| 305 | .update(cx, |state, cx| { |
| 306 | if state.google_connect_generation != generation { |
| 307 | return false; |
| 308 | } |
| 309 | state.google_connect_pending = Some(authorize_url.clone()); |
| 310 | crate::external_browser::open_url(cx, &state.store, &authorize_url); |
| 311 | cx.notify(); |
| 312 | true |
| 313 | }) |
| 314 | .unwrap_or(false); |
| 315 | if !still_current { |
| 316 | return; |
| 317 | } |
| 318 | let finished = runtime::spawn(async move { |
| 319 | finish_google_connect(oauth, pending, listener, store).await |
| 320 | }); |
| 321 | match finished.await { |
| 322 | Ok(Ok(email)) => { |
| 323 | this.update(cx, |state, cx| { |
| 324 | if state.google_connect_generation != generation { |
| 325 | return; |
| 326 | } |
| 327 | state.google_connect_pending = None; |
| 328 | let mut args = std::collections::HashMap::new(); |
| 329 | args.insert("email".to_owned(), email); |
| 330 | state.raise_notice(tr_args("settings.calendar.connected", &args), None, cx); |
| 331 | state.refresh_calendar(cx); |
| 332 | cx.notify(); |
| 333 | }) |
| 334 | .ok(); |
| 335 | } |
| 336 | Ok(Err(error)) => { |
| 337 | this.update(cx, |state, cx| { |
| 338 | if state.google_connect_generation != generation { |
| 339 | return; |
| 340 | } |
| 341 | state.google_connect_pending = None; |
| 342 | state.raise_notice(error, None, cx); |
| 343 | cx.notify(); |
| 344 | }) |
| 345 | .ok(); |
| 346 | } |
| 347 | Err(_) => { |
| 348 | this.update(cx, |state, cx| { |
| 349 | if state.google_connect_generation != generation { |
| 350 | return; |
| 351 | } |
| 352 | state.google_connect_pending = None; |
| 353 | state.raise_notice(tr("settings.calendar.sign_in_cancelled"), None, cx); |
| 354 | cx.notify(); |
| 355 | }) |
| 356 | .ok(); |
| 357 | } |
| 358 | } |
| 359 | }) |
| 360 | .detach(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | fn clear_google_connect_if(state: &mut AppState, generation: u64) { |
| 365 | if state.google_connect_generation == generation { |
| 366 | state.google_connect_pending = None; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | async fn finish_google_connect( |
| 371 | oauth: GoogleOAuth, |
| 372 | pending: chatsvc::AuthPending, |
| 373 | listener: tokio::net::TcpListener, |
| 374 | store: std::sync::Arc<store::Store>, |
| 375 | ) -> Result<String, String> { |
| 376 | let code = wait_for_code(&pending, listener) |
| 377 | .await |
| 378 | .map_err(|error| error.to_string())?; |
| 379 | let bundle = exchange_code(&oauth, &pending, &code) |
| 380 | .await |
| 381 | .map_err(|error| error.to_string())?; |
| 382 | let client = GoogleCalendar::new(bundle.access_token.clone()); |
| 383 | let list = client |
| 384 | .list_calendars() |
| 385 | .await |
| 386 | .map_err(|error| error.to_string())?; |
| 387 | let mut calendars = parse_calendar_list(&list) |
| 388 | .into_iter() |
| 389 | .map(|(id, summary, primary)| GoogleCalendarInfo { |
| 390 | id, |
| 391 | summary, |
| 392 | selected: true, |
| 393 | primary, |
| 394 | }) |
| 395 | .collect::<Vec<_>>(); |
| 396 | if calendars.is_empty() { |
| 397 | calendars.push(GoogleCalendarInfo { |
| 398 | id: "primary".into(), |
| 399 | summary: "Primary".into(), |
| 400 | selected: true, |
| 401 | primary: true, |
| 402 | }); |
| 403 | } |
| 404 | let email = calendars |
| 405 | .iter() |
| 406 | .find(|calendar| calendar.primary) |
| 407 | .map(|calendar| calendar.summary.clone()) |
| 408 | .unwrap_or_else(|| "Google".into()); |
| 409 | let account = GoogleAccount { |
| 410 | id: Uuid::new_v4().to_string(), |
| 411 | email: email.clone(), |
| 412 | calendars, |
| 413 | tokens: GoogleTokenSet { |
| 414 | access_token: bundle.access_token, |
| 415 | refresh_token: bundle.refresh_token, |
| 416 | expires_at: Utc::now().timestamp() + bundle.expires_in.max(60), |
| 417 | }, |
| 418 | }; |
| 419 | let mut accounts = load_calendar_accounts(&store); |
| 420 | accounts.connect_google(account); |
| 421 | save_calendar_accounts(&store, &accounts).map_err(|error| error.to_string())?; |
| 422 | Ok(email) |
| 423 | } |