use crate::{methods, util, Result};
use dbus::{
arg::{Append, Arg, Get, PropMap},
nonblock::{Proxy, SyncConnection},
};
use std::{fmt::Display, time::Duration};
#[derive(Clone)]
pub struct Player<'a> {
pub name: String,
conn: &'a SyncConnection,
}
impl<'a> Player<'a> {
pub async fn try_new<T>(name: T, conn: &'a SyncConnection) -> Result<Player<'a>>
where
T: AsRef<str> + Display,
{
if !util::validate(name.as_ref(), conn).await? {
return Err(Box::from("The provided player was invalid."));
}
let player = Player {
name: name.to_string(),
conn,
};
Ok(player)
}
#[doc(hidden)]
pub fn get_proxy(&mut self) -> Result<Proxy<&'a SyncConnection>> {
let proxy = Proxy::new(
format!("org.mpris.MediaPlayer2.{}", self.name),
"/org/mpris/MediaPlayer2",
Duration::from_millis(5000),
self.conn,
);
Ok(proxy)
}
pub async fn next(&mut self) -> Result<()> {
Ok(methods::next(self).await?)
}
pub async fn previous(&mut self) -> Result<()> {
Ok(methods::previous(self).await?)
}
pub async fn pause(&mut self) -> Result<()> {
Ok(methods::pause(self).await?)
}
pub async fn play(&mut self) -> Result<()> {
Ok(methods::play(self).await?)
}
pub async fn play_pause(&mut self) -> Result<()> {
Ok(methods::play_pause(self).await?)
}
pub async fn stop(&mut self) -> Result<()> {
Ok(methods::stop(self).await?)
}
pub async fn get_metadata(&mut self) -> Result<PropMap> {
Ok(methods::get_metadata(self).await?)
}
pub async fn get_property<T>(&mut self, property: &str) -> Result<T>
where
T: for<'c> Get<'c> + 'static,
{
Ok(methods::get_property(self, property).await?)
}
pub async fn set_property<T>(&mut self, property: &str, value: T) -> Result<()>
where
T: Arg + Append,
{
Ok(methods::set_property(self, property, value).await?)
}
pub async fn seek(&mut self, offset: Duration) -> Result<()> {
Ok(methods::seek(self, offset).await?)
}
pub async fn seek_reverse(&mut self, offset: Duration) -> Result<()> {
Ok(methods::seek_reverse(self, offset).await?)
}
pub async fn set_position(&mut self, position: i64) -> Result<()> {
Ok(methods::set_position(self, position).await?)
}
pub async fn open_uri(&mut self, uri: &str) -> Result<()> {
Ok(methods::open_uri(self, uri).await?)
}
}