From a0231362f10001496b9b5826a9c7808975db713c Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Mon, 22 Mar 2021 21:56:22 +0100 Subject: [PATCH] implement `logout` command fixes #470 --- src/command.rs | 3 +++ src/commands.rs | 10 ++++++++++ src/spotify.rs | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/src/command.rs b/src/command.rs index 962c285..3a7547b 100644 --- a/src/command.rs +++ b/src/command.rs @@ -134,6 +134,7 @@ pub enum Command { Insert(Option), NewPlaylist(String), Sort(SortKey, SortDirection), + Logout, } impl fmt::Display for Command { @@ -192,6 +193,7 @@ impl fmt::Display for Command { Command::Insert(_) => "insert".to_string(), Command::NewPlaylist(name) => format!("new playlist {}", name), Command::Sort(key, direction) => format!("sort {} {}", key, direction), + Command::Logout => "logout".to_string(), }; write!(f, "{}", repr) } @@ -415,6 +417,7 @@ pub fn parse(input: &str) -> Option { None } } + "logout" => Some(Command::Logout), "noop" => Some(Command::Noop), _ => None, } diff --git a/src/commands.rs b/src/commands.rs index 29c011b..d85eada 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -233,6 +233,16 @@ impl CommandManager { }); Ok(None) } + Command::Logout => { + self.spotify.shutdown(); + + let mut credentials_path = crate::config::cache_path("librespot"); + credentials_path.push("credentials.json"); + std::fs::remove_file(credentials_path).unwrap(); + + s.quit(); + Ok(None) + } Command::Jump(_) | Command::Move(_, _) | Command::Shift(_, _) diff --git a/src/spotify.rs b/src/spotify.rs index 8e6d35c..9a31175 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -71,6 +71,7 @@ enum WorkerCommand { Seek(u32), SetVolume(u16), RequestToken(oneshot::Sender), + Shutdown, } #[derive(Clone, Debug, PartialEq)] @@ -191,6 +192,10 @@ impl futures::Future for Worker { self.token_task = Spotify::get_token(&self.session, sender); progress = true; } + WorkerCommand::Shutdown => { + self.player.stop(); + self.session.shutdown(); + } } } @@ -943,6 +948,10 @@ impl Spotify { self.cfg.with_state_mut(|mut s| s.volume = volume); self.send_worker(WorkerCommand::SetVolume(Self::log_scale(volume))); } + + pub fn shutdown(&self) { + self.send_worker(WorkerCommand::Shutdown); + } } #[derive(Debug, PartialEq)]