]>
Commit | Line | Data |
---|---|---|
1 | use std::io::{Result, Error, ErrorKind::Other}; | |
2 | ||
3 | #[cfg(target_os = "macos")] | |
4 | mod apple_music; | |
5 | #[cfg(target_os = "macos")] | |
6 | mod spotify; | |
7 | ||
8 | // #[cfg(not(target_os = "macos"))] | |
9 | // mod rhythmbox; | |
10 | // #[cfg(not(target_os = "macos"))] | |
11 | // mod quod_libe; | |
12 | // #[cfg(not(target_os = "macos"))] | |
13 | // mod strawberry; | |
14 | // #[cfg(not(target_os = "macos"))] | |
15 | // mod tauon; | |
16 | ||
17 | #[cfg(target_os = "macos")] | |
18 | use apple_music::AppleMusic; | |
19 | #[cfg(target_os = "macos")] | |
20 | use spotify::Spotify; | |
21 | ||
22 | // #[cfg(not(target_os = "macos"))] | |
23 | // use rhythmbox::Rhythmbox; | |
24 | // #[cfg(not(target_os = "macos"))] | |
25 | // use quod_libet::QuodLibet; | |
26 | // #[cfg(not(target_os = "macos"))] | |
27 | // use strawberry::Strawberry; | |
28 | // #[cfg(not(target_os = "macos"))] | |
29 | // use tauon::Tauon; | |
30 | ||
31 | use crate::Track; | |
32 | ||
33 | pub trait LyricsSource { | |
34 | fn name(&self) -> String; | |
35 | ||
36 | fn current_track(&self) -> Option<Track>; | |
37 | ||
38 | fn disable(&self) -> Result<()>; | |
39 | fn enable(&self) -> Result<()>; | |
40 | fn reset(&self) -> Result<()>; | |
41 | } | |
42 | ||
43 | pub fn list() -> Vec<String> { | |
44 | available_sources().into_iter().map(|source| source.name()).collect() | |
45 | } | |
46 | ||
47 | pub fn enable(source_name: &String) -> Result<()> { | |
48 | let sources = available_sources(); | |
49 | for source in sources { | |
50 | if &source.name() == source_name { | |
51 | return source.enable() | |
52 | } | |
53 | } | |
54 | Err(Error::new(Other, "No such source was available.")) | |
55 | } | |
56 | ||
57 | pub fn disable(source_name: &String) -> Result<()> { | |
58 | let sources = available_sources(); | |
59 | for source in sources { | |
60 | if &source.name() == source_name { | |
61 | return source.disable() | |
62 | } | |
63 | } | |
64 | Err(Error::new(Other, "No such source was available.")) | |
65 | } | |
66 | ||
67 | pub fn reset(source_name: &String) -> Result<()> { | |
68 | let sources = available_sources(); | |
69 | for source in sources { | |
70 | if &source.name() == source_name { | |
71 | return source.reset() | |
72 | } | |
73 | } | |
74 | Err(Error::new(Other, "No such source was available.")) | |
75 | } | |
76 | ||
77 | pub fn get_track() -> Option<Track> { | |
78 | let sources = available_sources(); | |
79 | for source in sources { | |
80 | if let Some(track) = source.current_track() { | |
81 | return Some(track); | |
82 | } | |
83 | } | |
84 | return None | |
85 | } | |
86 | ||
87 | pub fn available_sources() -> Vec<Box<dyn LyricsSource>> { | |
88 | let mut sources: Vec<Box<dyn LyricsSource>> = Vec::new(); | |
89 | #[cfg(target_os = "macos")] | |
90 | { | |
91 | sources.push(Box::new(AppleMusic::new())); | |
92 | sources.push(Box::new(Spotify::new())); | |
93 | } | |
94 | ||
95 | #[cfg(not(target_os = "macos"))] | |
96 | { | |
97 | // sources.push(Box::new(Rhythmbox::new())); | |
98 | // sources.push(Box::new(QuodLibet::new())); | |
99 | // sources.push(Box::new(Strawberry::new())); | |
100 | // sources.push(Box::new(Tauon::new())); | |
101 | } | |
102 | ||
103 | sources | |
104 | } |