Skip to content

Commit 397d836

Browse files
committed
Improve player name detection again
Now returns the whole bus name not just the last part and the implementation assumption got documented.
1 parent 502f4d3 commit 397d836

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/player.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,21 @@ impl Player {
107107
}
108108

109109
/// Returns the player name part of the player's D-Bus bus name.
110-
/// This is the part after "org.mpris.MediaPlayer2.", not including Flatpak name parts and the instance part.
110+
/// This is the part after "org.mpris.MediaPlayer2.", not including the instance part.
111+
///
112+
/// **Note**: The instance part removal isn't guaranteed to work. It assumes that the player
113+
/// implements it as mentioned in the MPRIS specification example.
111114
///
112115
/// See: [MPRIS2 specification about bus names][bus_names].
113116
///
114117
/// [bus_names]: https://specifications.freedesktop.org/mpris-spec/latest/#Bus-Name-Policy
115118
pub fn bus_name_player_name_part(&self) -> &str {
116-
let bus_split: Vec<&str> = self
117-
.bus_name
118-
.trim_start_matches(MPRIS2_PREFIX)
119-
.rsplit('.')
120-
.collect();
121-
122-
if bus_split.len() == 1 || bus_split.len() == 3 {
123-
// Safety in case a player's name actually starts with "instance"
124-
bus_split[0]
125-
} else if bus_split[0].starts_with("instance") {
126-
bus_split[1]
119+
let bus_trimmed = self.bus_name.trim_start_matches(MPRIS2_PREFIX);
120+
121+
if let Some(find_index) = bus_trimmed.rfind(".instance") {
122+
&bus_trimmed[0..find_index]
127123
} else {
128-
bus_split[0]
124+
bus_trimmed
129125
}
130126
}
131127

0 commit comments

Comments
 (0)