diff --git a/src/music_track_listener.py b/src/music_track_listener.py index b5bccbdc4..c640a42c2 100644 --- a/src/music_track_listener.py +++ b/src/music_track_listener.py @@ -24,8 +24,9 @@ class MusicTrackInfo(object): class MusicTrackListener(gobject.GObject): - __gsignals__ = { 'music-track-changed': (gobject.SIGNAL_RUN_LAST, None, - (object,)) } + __gsignals__ = { + 'music-track-changed': (gobject.SIGNAL_RUN_LAST, None, (object,)), + } _instance = None @classmethod @@ -36,14 +37,39 @@ class MusicTrackListener(gobject.GObject): def __init__(self): super(MusicTrackListener, self).__init__() + self._last_playing_music = None + bus = dbus.SessionBus() + + ## Muine bus.add_signal_receiver(self._muine_music_track_change_cb, 'SongChanged', 'org.gnome.Muine.Player') + bus.add_signal_receiver(self._player_name_owner_changed, + 'NameOwnerChanged', 'org.freedesktop.DBus', arg0='org.gnome.Muine') + bus.add_signal_receiver(self._player_playing_changed_cb, 'StateChanged', + 'org.gnome.Muine.Player') + + ## Rhythmbox bus.add_signal_receiver(self._rhythmbox_music_track_change_cb, 'playingUriChanged', 'org.gnome.Rhythmbox.Player') + bus.add_signal_receiver(self._player_name_owner_changed, + 'NameOwnerChanged', 'org.freedesktop.DBus', arg0='org.gnome.Rhythmbox') + bus.add_signal_receiver(self._player_playing_changed_cb, + 'playingChanged', 'org.gnome.Rhythmbox.Player') + + def _player_name_owner_changed(self, name, old, new): + if not new: + self.emit('music-track-changed', None) + + def _player_playing_changed_cb(self, playing): + if playing: + self.emit('music-track-changed', self._last_playing_music) + else: + self.emit('music-track-changed', None) def _muine_properties_extract(self, song_string): - d = dict((x.strip() for x in s1.split(':', 1)) for s1 in song_string.split('\n')) + d = dict((x.strip() for x in s1.split(':', 1)) for s1 in \ + song_string.split('\n')) info = MusicTrackInfo() info.title = d['title'] info.album = d['album'] @@ -67,7 +93,8 @@ class MusicTrackListener(gobject.GObject): def _rhythmbox_music_track_change_cb(self, uri): bus = dbus.SessionBus() - rbshellobj = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell') + rbshellobj = bus.get_object('org.gnome.Rhythmbox', + '/org/gnome/Rhythmbox/Shell') rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell') props = rbshell.getSongProperties(uri) info = self._rhythmbox_properties_extract(props) @@ -87,19 +114,22 @@ class MusicTrackListener(gobject.GObject): if player.GetPlaying(): song_string = player.GetCurrentSong() song = self._muine_properties_extract(song_string) + self._last_playing_music = song return song ## Check Rhythmbox playing song if dbus.dbus_bindings.bus_name_has_owner(bus.get_connection(), 'org.gnome.Rhythmbox'): - rbshellobj = bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell') + rbshellobj = bus.get_object('org.gnome.Rhythmbox', + '/org/gnome/Rhythmbox/Shell') player = dbus.Interface( - bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player'), - 'org.gnome.Rhythmbox.Player') + bus.get_object('org.gnome.Rhythmbox', + '/org/gnome/Rhythmbox/Player'), 'org.gnome.Rhythmbox.Player') rbshell = dbus.Interface(rbshellobj, 'org.gnome.Rhythmbox.Shell') uri = player.getPlayingUri() props = rbshell.getSongProperties(uri) info = self._rhythmbox_properties_extract(props) + self._last_playing_music = info return info return None @@ -107,7 +137,10 @@ class MusicTrackListener(gobject.GObject): # here we test :) if __name__ == '__main__': def music_track_change_cb(listener, music_track_info): - print music_track_info.title + if music_track_info is None: + print "Stop!" + else: + print music_track_info.title listener = MusicTrackListener.get() listener.connect('music-track-changed', music_track_change_cb) track = listener.get_playing_track()