Skip to content

Commit 7fc4159

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

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/mpris_event_handler.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,17 @@ 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");
626626
// FIXME: This should return an error if can_pause is false
627-
if let Some(spirc) = &self.spirc {
628-
let _ = spirc.pause();
627+
match (&self.spirc, &self.metadata.mpris.track_id) {
628+
(Some(spirc), Some(_)) => spirc
629+
.pause()
630+
.map_err(|err| zbus::fdo::Error::Failed(format!("{err}"))),
631+
(Some(_), None) => {
632+
zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("No track")))
633+
}
634+
_ => zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("Can't play/pause"))),
629635
}
630636
}
631637

@@ -637,11 +643,16 @@ impl MprisPlayerService {
637643
//
638644
// If `self.can_pause` is `false`, attempting to call this method should have no effect and
639645
// raise an error.
640-
async fn play_pause(&self) {
646+
async fn play_pause(&self) -> zbus::fdo::Result<()> {
641647
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();
648+
match (&self.spirc, &self.metadata.mpris.track_id) {
649+
(Some(spirc), Some(_)) => spirc
650+
.play_pause()
651+
.map_err(|err| zbus::fdo::Error::Failed(format!("{err}"))),
652+
(Some(_), None) => {
653+
zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("No track")))
654+
}
655+
_ => zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("Can't play/pause"))),
645656
}
646657
}
647658

@@ -656,7 +667,6 @@ impl MprisPlayerService {
656667
// an error.
657668
async fn stop(&self) {
658669
debug!("org.mpris.MediaPlayer2.Player::Stop");
659-
// FIXME: This should return an error if can_control is false
660670
if let Some(spirc) = &self.spirc {
661671
let _ = spirc.pause();
662672
let _ = spirc.set_position_ms(0);
@@ -672,12 +682,25 @@ impl MprisPlayerService {
672682
// If there is no track to play, this has no effect.
673683
//
674684
// If `self.can_play` is `false`, attempting to call this method should have no effect.
675-
async fn play(&self) {
685+
async fn play(&self) -> zbus::fdo::Result<()> {
676686
debug!("org.mpris.MediaPlayer2.Player::Play");
677687
if let Some(spirc) = &self.spirc {
678688
let _ = spirc.activate();
679689
let _ = spirc.play();
680690
}
691+
match (&self.spirc, &self.metadata.mpris.track_id) {
692+
(Some(spirc), Some(_)) => {
693+
let result: Result<(), Error> = (|| {
694+
spirc.activate()?;
695+
spirc.play()
696+
})();
697+
result.map_err(|err| zbus::fdo::Error::Failed(format!("{err}")))
698+
}
699+
(Some(_), None) => {
700+
zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("No track")))
701+
}
702+
_ => zbus::fdo::Result::Err(zbus::fdo::Error::Failed(String::from("Can't play/pause"))),
703+
}
681704
}
682705

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

0 commit comments

Comments
 (0)