aboutsummaryrefslogtreecommitdiff
path: root/src/sources/swinsian.rs
blob: 641af6fa188be661eb551de43b46d6b36a513e5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::io::Result;

use objc2::{msg_send, rc::Retained, runtime::AnyObject};
use objc2_foundation::NSString;
use objc2_scripting_bridge::SBApplication;

use crate::Track;

use super::LyricsSource;

pub struct Swinsian;

impl Swinsian {
    pub fn new() -> Self {
        Swinsian
    }
}

impl LyricsSource for Swinsian {
    fn name(&self) -> String {
        "swinsian".to_string()
    }

    fn current_track(&self) -> Option<Track> {
        unsafe {
            let bundle_identifier = NSString::from_str("com.swinsian.Swinsian");
            if let Some(app) = SBApplication::applicationWithBundleIdentifier(&bundle_identifier) {
                let key = NSString::from_str("currentTrack");
                let current_track: Option<Retained<AnyObject>> = msg_send![&app, valueForKey:&*key];

                if let Some(track) = current_track {
                    let name_key = NSString::from_str("name");
                    let artist_key = NSString::from_str("artist");

                    let name: Option<Retained<NSString>> =
                        msg_send![&track, valueForKey:&*name_key];
                    let artist: Option<Retained<NSString>> =
                        msg_send![&track, valueForKey:&*artist_key];

                    if let Some(name) = name.map(|s| s.to_string())
                        && let Some(artist) = artist.map(|s| s.to_string())
                    {
                        return Some(Track { name, artist });
                    }
                }
            }
        }
        None
    }

    fn disable(&self) -> Result<()> {
        Ok(())
    }

    fn enable(&self) -> Result<()> {
        Ok(())
    }

    fn reset(&self) -> Result<()> {
        Ok(())
    }
}