diff --git a/src/playbin/song.rs b/src/playbin/song.rs index fa33a47..0edadab 100644 --- a/src/playbin/song.rs +++ b/src/playbin/song.rs @@ -25,6 +25,8 @@ mod ffi { } } +use glib::translate::{from_glib_none, ToGlibPtr}; +use glib::GString; use gtk::glib; glib::wrapper! { @@ -36,33 +38,15 @@ glib::wrapper! { } impl Song { - pub fn title(&self) -> std::borrow::Cow<'_, str> { - use glib::translate::ToGlibPtr; - - // TODO: memory management.... - String::from_utf8_lossy(unsafe { - std::ffi::CStr::from_ptr(ffi::audrey_playbin_song_get_title(self.to_glib_none().0)) - .to_bytes() - }) + pub fn title(&self) -> GString { + unsafe { from_glib_none(ffi::audrey_playbin_song_get_title(self.to_glib_none().0)) } } - pub fn artist(&self) -> std::borrow::Cow<'_, str> { - use glib::translate::ToGlibPtr; - - // TODO: memory management.... - String::from_utf8_lossy(unsafe { - std::ffi::CStr::from_ptr(ffi::audrey_playbin_song_get_artist(self.to_glib_none().0)) - .to_bytes() - }) + pub fn artist(&self) -> GString { + unsafe { from_glib_none(ffi::audrey_playbin_song_get_artist(self.to_glib_none().0)) } } - pub fn album(&self) -> std::borrow::Cow<'_, str> { - use glib::translate::ToGlibPtr; - - // TODO: memory management.... - String::from_utf8_lossy(unsafe { - std::ffi::CStr::from_ptr(ffi::audrey_playbin_song_get_album(self.to_glib_none().0)) - .to_bytes() - }) + pub fn album(&self) -> GString { + unsafe { from_glib_none(ffi::audrey_playbin_song_get_album(self.to_glib_none().0)) } } } diff --git a/src/ui/playbar.rs b/src/ui/playbar.rs index fe60865..5999343 100644 --- a/src/ui/playbar.rs +++ b/src/ui/playbar.rs @@ -2,6 +2,7 @@ mod imp { use adw::prelude::*; use adw::subclass::prelude::*; use glib::subclass::InitializingObject; + use glib::{gformat, gstr, GString}; use gtk::{gdk, glib}; use std::cell::{Cell, RefCell}; @@ -48,32 +49,23 @@ mod imp { #[gtk::template_callbacks] impl Playbar { #[template_callback] - fn song_title(&self, song: Option<&crate::playbin::Song>) -> String { - match song { - None => "".to_owned(), - Some(song) => song.title().to_string(), - } + fn song_title(&self, song: Option<&crate::playbin::Song>) -> Option { + song.map(|song| song.title()) } #[template_callback] - fn song_artist(&self, song: Option<&crate::playbin::Song>) -> String { - match song { - None => "".to_owned(), - Some(song) => song.artist().to_string(), - } + fn song_artist(&self, song: Option<&crate::playbin::Song>) -> Option { + song.map(|song| song.artist()) } #[template_callback] - fn song_album(&self, song: Option<&crate::playbin::Song>) -> String { - match song { - None => "".to_owned(), - Some(song) => song.album().to_string(), - } + fn song_album(&self, song: Option<&crate::playbin::Song>) -> Option { + song.map(|song| song.album()) } #[template_callback] - fn format_timestamp(&self, s: f64) -> String { - format!("{:02}:{:02}", (s as i64) / 64, (s as i64) % 60) + fn format_timestamp(&self, s: f64) -> GString { + gformat!("{:02}:{:02}", (s as i64) / 64, (s as i64) % 60) } #[template_callback]