pris/methods/
methods_complex.rs

1use super::INTERFACE;
2use crate::{Player, Result};
3use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties;
4use dbus::{
5    arg::{Append, Arg, Get, PropMap},
6    strings::Path,
7};
8use std::time::Duration;
9
10/// Retrieves track metadata from a `Player`.
11/// The [`prop_cast`](crate::prop_cast) function may be used
12/// to get specific values out of the resulting metadata.
13///
14/// # Errors
15/// May `Err` if there is a failure in getting the metadata.
16pub async fn get_metadata(player: &mut Player<'_>) -> Result<PropMap> {
17    let proxy = player.get_proxy()?;
18    let metadata: PropMap = proxy.get(INTERFACE, "Metadata").await?;
19    Ok(metadata)
20}
21
22/// Retrieves the value of an MPRIS property.
23/// Available properties can be found [here].
24///
25/// [here]: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:PlaybackStatus
26///
27/// # Errors
28/// May return an `Err` variant if:
29/// * An invalid type was provided for the property
30/// * An invalid property was provided
31pub async fn get_property<T>(player: &mut Player<'_>, property: &str) -> Result<T>
32where
33    T: for<'a> Get<'a> + 'static,
34{
35    let proxy = player.get_proxy()?;
36    let value: T = proxy.get(INTERFACE, property).await?;
37
38    Ok(value)
39}
40
41/// Sets the value of a writable MPRIS property.
42/// Available properties can be found [here].
43///
44/// [here]: https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html#Property:PlaybackStatus
45///
46/// # Errors
47/// May return an `Err` variant if:
48/// * An invalid type was provided for the property
49/// * An invalid property was provided
50pub async fn set_property<T>(player: &mut Player<'_>, property: &str, value: T) -> Result<()>
51where
52    T: Arg + Append,
53{
54    let proxy = player.get_proxy()?;
55    proxy.set(INTERFACE, property, value).await?;
56
57    Ok(())
58}
59
60/// Seeks the position of the active track.
61pub async fn seek(player: &mut Player<'_>, offset: Duration) -> Result<()> {
62    let proxy = player.get_proxy()?;
63    let offset = offset.as_micros() as i64;
64    proxy.method_call(INTERFACE, "Seek", (offset,)).await?;
65
66    Ok(())
67}
68
69/// Same as `seek`, but in reverse.
70pub async fn seek_reverse(player: &mut Player<'_>, offset: Duration) -> Result<()> {
71    let proxy = player.get_proxy()?;
72    let offset = offset.as_micros() as i64;
73    proxy.method_call(INTERFACE, "Seek", (-offset,)).await?;
74
75    Ok(())
76}
77
78/// Sets the position of the current track, by microseconds.
79pub async fn set_position(player: &mut Player<'_>, position: i64) -> Result<()> {
80    let mut player_clone = player.clone();
81
82    let proxy = player.get_proxy()?;
83    let metadata = get_metadata(&mut player_clone).await?;
84    let track_id: &Path = crate::prop_cast(&metadata, "mpris:trackid").unwrap();
85
86    proxy
87        .method_call(INTERFACE, "SetPosition", (track_id, position))
88        .await?;
89
90    Ok(())
91}
92
93/// Opens a track by its URI.
94///
95/// # Errors
96/// May return an `Err` variant if the provided URI is invalid.
97pub async fn open_uri(player: &mut Player<'_>, uri: &str) -> Result<()> {
98    let proxy = player.get_proxy()?;
99    proxy.method_call(INTERFACE, "OpenUri", (uri,)).await?;
100
101    Ok(())
102}