Skip to content

Commit f6481fd

Browse files
committed
feat(mpris): Return error when trying to play/pause in wrong context
1 parent 274d001 commit f6481fd

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/mpris_event_handler.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,16 @@ impl MprisPlayerService {
621621
// Calling Play after this should cause playback to start again from the same position.
622622
//
623623
// If `self.can_pause` is `false`, attempting to call this method should have no effect.
624-
async fn pause(&self) {
624+
async fn pause(&self) -> zbus::fdo::Result<()> {
625625
debug!("org.mpris.MediaPlayer2.Player::Pause");
626-
// FIXME: This should return an error if can_pause is false
627-
if let Some(spirc) = &self.spirc {
628-
let _ = spirc.pause();
626+
match (&self.spirc, &self.metadata.mpris.track_id) {
627+
(Some(spirc), Some(_)) => spirc
628+
.pause()
629+
.map_err(|err| zbus::fdo::Error::Failed(format!("{err}"))),
630+
(Some(_), None) => {
631+
zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("No track")))
632+
}
633+
_ => zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("Can't play/pause"))),
629634
}
630635
}
631636

@@ -637,11 +642,16 @@ impl MprisPlayerService {
637642
//
638643
// If `self.can_pause` is `false`, attempting to call this method should have no effect and
639644
// raise an error.
640-
async fn play_pause(&self) {
645+
async fn play_pause(&self) -> zbus::fdo::Result<()> {
641646
debug!("org.mpris.MediaPlayer2.Player::PlayPause");
642-
// FIXME: This should return an error if can_pause is false
643-
if let Some(spirc) = &self.spirc {
644-
let _ = spirc.play_pause();
647+
match (&self.spirc, &self.metadata.mpris.track_id) {
648+
(Some(spirc), Some(_)) => spirc
649+
.play_pause()
650+
.map_err(|err| zbus::fdo::Error::Failed(format!("{err}"))),
651+
(Some(_), None) => {
652+
zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("No track")))
653+
}
654+
_ => zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("Can't play/pause"))),
645655
}
646656
}
647657

@@ -656,7 +666,6 @@ impl MprisPlayerService {
656666
// an error.
657667
async fn stop(&self) {
658668
debug!("org.mpris.MediaPlayer2.Player::Stop");
659-
// FIXME: This should return an error if can_control is false
660669
if let Some(spirc) = &self.spirc {
661670
let _ = spirc.pause();
662671
let _ = spirc.set_position_ms(0);
@@ -672,12 +681,25 @@ impl MprisPlayerService {
672681
// If there is no track to play, this has no effect.
673682
//
674683
// If `self.can_play` is `false`, attempting to call this method should have no effect.
675-
async fn play(&self) {
684+
async fn play(&self) -> zbus::fdo::Result<()> {
676685
debug!("org.mpris.MediaPlayer2.Player::Play");
677686
if let Some(spirc) = &self.spirc {
678687
let _ = spirc.activate();
679688
let _ = spirc.play();
680689
}
690+
match (&self.spirc, &self.metadata.mpris.track_id) {
691+
(Some(spirc), Some(_)) => {
692+
let result: Result<(), Error> = (|| {
693+
spirc.activate()?;
694+
spirc.play()
695+
})();
696+
result.map_err(|err| zbus::fdo::Error::Failed(format!("{err}")))
697+
}
698+
(Some(_), None) => {
699+
zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("No track")))
700+
}
701+
_ => zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("Can't play/pause"))),
702+
}
681703
}
682704

683705
// Seeks forward in the current track by the specified number of microseconds.

0 commit comments

Comments
 (0)