27
src/main.rs
27
src/main.rs
@@ -98,12 +98,23 @@ fn setup_logging(filename: &str) -> Result<(), fern::InitError> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn credentials_prompt(reset: bool) -> Credentials {
|
fn credentials_prompt(reset: bool, error_message: Option<String>) -> Credentials {
|
||||||
let path = config::config_path("credentials.toml");
|
let path = config::config_path("credentials.toml");
|
||||||
if reset && fs::remove_file(&path).is_err() {
|
if reset && fs::remove_file(&path).is_err() {
|
||||||
error!("could not delete credential file");
|
error!("could not delete credential file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(message) = error_message {
|
||||||
|
let mut siv = cursive::default();
|
||||||
|
let dialog = cursive::views::Dialog::around(cursive::views::TextView::new(format!(
|
||||||
|
"Connection error:\n{}",
|
||||||
|
message
|
||||||
|
)))
|
||||||
|
.button("Ok", |s| s.quit());
|
||||||
|
siv.add_layer(dialog);
|
||||||
|
siv.run();
|
||||||
|
}
|
||||||
|
|
||||||
let creds =
|
let creds =
|
||||||
crate::config::load_or_generate_default(&path, authentication::create_credentials, true)
|
crate::config::load_or_generate_default(&path, authentication::create_credentials, true)
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
@@ -184,12 +195,20 @@ fn main() {
|
|||||||
info!("Using cached credentials");
|
info!("Using cached credentials");
|
||||||
c
|
c
|
||||||
}
|
}
|
||||||
None => credentials_prompt(false),
|
None => credentials_prompt(false, None),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
while !spotify::Spotify::test_credentials(credentials.clone()) {
|
while let Err(error) = spotify::Spotify::test_credentials(credentials.clone()) {
|
||||||
credentials = credentials_prompt(true);
|
let reset = error
|
||||||
|
.get_ref()
|
||||||
|
.map_or(false, |err| err.to_string().contains("BadCredentials"));
|
||||||
|
debug!("credential reset: {:?}", reset);
|
||||||
|
let error_msg = match error.get_ref() {
|
||||||
|
Some(inner) => inner.to_string(),
|
||||||
|
None => error.to_string(),
|
||||||
|
};
|
||||||
|
credentials = credentials_prompt(reset, Some(error_msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
let theme = theme::load(&cfg);
|
let theme = theme::load(&cfg);
|
||||||
|
|||||||
@@ -44,12 +44,12 @@ use url::Url;
|
|||||||
|
|
||||||
use core::task::Poll;
|
use core::task::Poll;
|
||||||
|
|
||||||
use std::env;
|
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::atomic::{AtomicU16, Ordering};
|
use std::sync::atomic::{AtomicU16, Ordering};
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
use std::{env, io};
|
||||||
|
|
||||||
use crate::artist::Artist;
|
use crate::artist::Artist;
|
||||||
use crate::config;
|
use crate::config;
|
||||||
@@ -329,15 +329,23 @@ impl Spotify {
|
|||||||
session_config
|
session_config
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_credentials(credentials: Credentials) -> bool {
|
pub fn test_credentials(credentials: Credentials) -> Result<Session, std::io::Error> {
|
||||||
let th = thread::spawn(move || {
|
let jh = thread::spawn(move || {
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
let config = Self::session_config();
|
let config = Self::session_config();
|
||||||
let handle = core.handle();
|
let handle = core.handle();
|
||||||
|
|
||||||
core.run(Session::connect(config, credentials, None, handle))
|
core.run(Session::connect(config, credentials, None, handle))
|
||||||
});
|
});
|
||||||
th.join().is_ok()
|
match jh.join() {
|
||||||
|
Ok(session) => session.or_else(Err),
|
||||||
|
Err(e) => Err(io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
e.downcast_ref::<String>()
|
||||||
|
.unwrap_or(&"N/A".to_string())
|
||||||
|
.to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_session(core: &mut Core, cfg: &config::Config, credentials: Credentials) -> Session {
|
fn create_session(core: &mut Core, cfg: &config::Config, credentials: Credentials) -> Session {
|
||||||
|
|||||||
Reference in New Issue
Block a user