Skip to content

Commit

Permalink
move from xdg crate to cross-platform directories crate
Browse files Browse the repository at this point in the history
closes #38
  • Loading branch information
hrkfdn committed Mar 20, 2019
1 parent 2b45778 commit cdf63ba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ maintenance = { status = "experimental" }

[dependencies]
crossbeam-channel = "0.3.8"
directories = "1.0"
failure = "0.1.3"
futures = "0.1"
log = "0.4.0"
Expand All @@ -25,7 +26,6 @@ tokio-core = "0.1"
tokio-timer = "0.2"
unicode-width = "0.1.5"
dbus = { version = "0.6.4", optional = true }
xdg = "^2.1"

[dependencies.rspotify]
git = "https://github.com/hrkfdn/rspotify.git"
Expand Down
26 changes: 26 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use directories::ProjectDirs;

pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b";

Expand All @@ -8,3 +12,25 @@ pub struct Config {
pub password: String,
pub keybindings: Option<HashMap<String, String>>,
}

pub fn config_path() -> PathBuf {
let dirs = directories::BaseDirs::new().expect("can't determine config path");
PathBuf::from(format!(
"{0}/ncspot",
dirs.config_dir()
.to_str()
.expect("can't convert path to string")
))
}

pub fn cache_path() -> PathBuf {
let proj_dirs =
ProjectDirs::from("org", "affekt", "ncspot").expect("can't determine project paths");
let cache_dir = proj_dirs.cache_dir();
if !cache_dir.exists() {
fs::create_dir(cache_dir).expect("can't create cache folder");
}
let mut pb = proj_dirs.cache_dir().to_path_buf();
pb.push("playlists.db");
pb
}
12 changes: 2 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate crossbeam_channel;
#[macro_use]
extern crate cursive;
extern crate directories;
extern crate failure;
extern crate futures;
extern crate librespot;
Expand All @@ -9,7 +10,6 @@ extern crate tokio;
extern crate tokio_core;
extern crate tokio_timer;
extern crate unicode_width;
extern crate xdg;

#[cfg(feature = "mpris")]
extern crate dbus;
Expand All @@ -22,8 +22,6 @@ extern crate toml;
#[macro_use]
extern crate log;

use std::env;
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
use std::thread;
Expand Down Expand Up @@ -57,13 +55,7 @@ fn main() {

// Things here may cause the process to abort; we must do them before creating curses windows
// otherwise the error message will not be seen by a user
let path = match env::var_os("HOME") {
None => {
eprintln!("$HOME not set");
process::exit(1);
}
Some(path) => PathBuf::from(format!("{0}/.config/ncspot", path.into_string().unwrap())),
};
let path = config::config_path();

let cfg: config::Config = {
let contents = std::fs::read_to_string(&path).unwrap_or_else(|_| {
Expand Down
59 changes: 30 additions & 29 deletions src/playlists.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::iter::Iterator;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};

use rspotify::spotify::model::playlist::SimplifiedPlaylist;

use config;
use events::EventManager;
use queue::Queue;
use spotify::Spotify;
Expand All @@ -21,6 +23,7 @@ pub struct Playlists {
pub store: Arc<RwLock<Vec<Playlist>>>,
ev: EventManager,
spotify: Arc<Spotify>,
cache_path: PathBuf,
}

impl ListItem for Playlist {
Expand Down Expand Up @@ -51,43 +54,38 @@ impl Playlists {
store: Arc::new(RwLock::new(Vec::new())),
ev: ev.clone(),
spotify: spotify.clone(),
cache_path: config::cache_path(),
}
}

pub fn load_cache(&self) {
let xdg_dirs = xdg::BaseDirectories::with_prefix("ncspot").unwrap();
if let Ok(cache_path) = xdg_dirs.place_cache_file("playlists.db") {
if let Ok(contents) = std::fs::read_to_string(&cache_path) {
debug!(
"loading playlist cache from {}",
cache_path.to_str().unwrap()
);
let parsed: Result<Vec<Playlist>, _> = serde_json::from_str(&contents);
match parsed {
Ok(cache) => {
debug!("playlist cache loaded ({} lists)", cache.len());
let mut store = self.store.write().expect("can't writelock playlist store");
store.clear();
store.extend(cache);

// force refresh of UI (if visible)
self.ev.trigger();
}
Err(e) => {
error!("can't parse playlist cache: {}", e);
}
if let Ok(contents) = std::fs::read_to_string(&self.cache_path) {
debug!(
"loading playlist cache from {}",
self.cache_path.to_str().unwrap()
);
let parsed: Result<Vec<Playlist>, _> = serde_json::from_str(&contents);
match parsed {
Ok(cache) => {
debug!("playlist cache loaded ({} lists)", cache.len());
let mut store = self.store.write().expect("can't writelock playlist store");
store.clear();
store.extend(cache);

// force refresh of UI (if visible)
self.ev.trigger();
}
Err(e) => {
error!("can't parse playlist cache: {}", e);
}
}
}
}

pub fn save_cache(&self) {
let xdg_dirs = xdg::BaseDirectories::with_prefix("ncspot").unwrap();
if let Ok(cache_path) = xdg_dirs.place_cache_file("playlists.db") {
match serde_json::to_string(&self.store.deref()) {
Ok(contents) => std::fs::write(cache_path, contents).unwrap(),
Err(e) => error!("could not write playlist cache: {:?}", e),
}
match serde_json::to_string(&self.store.deref()) {
Ok(contents) => std::fs::write(&self.cache_path, contents).unwrap(),
Err(e) => error!("could not write playlist cache: {:?}", e),
}
}

Expand All @@ -108,8 +106,11 @@ impl Playlists {
tracks_result = match tracks.next {
Some(_) => {
debug!("requesting tracks again..");
spotify
.user_playlist_tracks(&id, 100, tracks.offset + tracks.items.len() as u32)
spotify.user_playlist_tracks(
&id,
100,
tracks.offset + tracks.items.len() as u32,
)
}
None => None,
}
Expand Down

0 comments on commit cdf63ba

Please sign in to comment.