music_track_listener: don't hard code media players

inspired by 0ba346b750/js/ui/mpris.js
This commit is contained in:
André Apitzsch 2018-09-02 00:46:37 +02:00 committed by Philipp Hörist
parent 3e73ee93e1
commit 64ee94fcd1
1 changed files with 60 additions and 32 deletions

View File

@ -30,20 +30,7 @@ from gi.repository import Gio, GLib
log = logging.getLogger('gajim.music_track_listener') log = logging.getLogger('gajim.music_track_listener')
MPRIS_PLAYER_PREFIX = 'org.mpris.MediaPlayer2.'
def _get_music_players():
players = [
'org.mpris.MediaPlayer2.audacious',
'org.mpris.MediaPlayer2.bmp',
'org.mpris.MediaPlayer2.clementine',
'org.mpris.MediaPlayer2.GnomeMusic',
'org.mpris.MediaPlayer2.quodlibet',
'org.mpris.MediaPlayer2.rhythmbox',
'org.mpris.MediaPlayer2.vlc',
'org.mpris.MediaPlayer2.xmms2'
]
return players
class MusicTrackInfo(object): class MusicTrackInfo(object):
@ -65,22 +52,63 @@ class MusicTrackListener(GObject.GObject):
return cls._instance return cls._instance
def __init__(self): def __init__(self):
super(MusicTrackListener, self).__init__() super().__init__()
self.con = {} self.players = {}
players = _get_music_players() proxy = Gio.DBusProxy.new_for_bus_sync(
for name in players:
Gio.bus_watch_name(
Gio.BusType.SESSION, Gio.BusType.SESSION,
name, Gio.DBusProxyFlags.NONE,
Gio.BusNameWatcherFlags.NONE, None,
self._appeared, 'org.freedesktop.DBus',
self._vanished) '/org/freedesktop/DBus',
'org.freedesktop.DBus',
None)
def _appeared(self, connection, name, name_owner, *user_data): self.connection = proxy.get_connection()
self.connection.signal_subscribe(
'org.freedesktop.DBus',
'org.freedesktop.DBus',
'NameOwnerChanged',
'/org/freedesktop/DBus',
None,
Gio.DBusSignalFlags.NONE,
self._signal_name_owner_changed)
try:
result = proxy.call_sync(
'ListNames',
None,
Gio.DBusCallFlags.NONE,
-1,
None)
except GLib.Error as e:
if e.domain == 'g-dbus-error-quark':
log.debug("Could not list names: %s", e.message)
return
else:
raise
for name in result[0]:
if name.startswith(MPRIS_PLAYER_PREFIX):
self._add_player(name)
def _signal_name_owner_changed(self, connection, sender_name, object_path,
interface_name, signal_name, parameters, *user_data):
name, oldOwner, newOwner = parameters
if name.startswith(MPRIS_PLAYER_PREFIX):
if newOwner and not oldOwner:
self._add_player(name)
else:
self._remove_player(name)
def _add_player(self, name):
'''Set up a listener for music player signals''' '''Set up a listener for music player signals'''
log.info('%s appeared', name) log.info('%s appeared', name)
self.con[name] = connection.signal_subscribe(
if name in self.players:
return
self.players[name] = self.connection.signal_subscribe(
name, name,
'org.freedesktop.DBus.Properties', 'org.freedesktop.DBus.Properties',
'PropertiesChanged', 'PropertiesChanged',
@ -94,12 +122,12 @@ class MusicTrackListener(GObject.GObject):
if info is not None: if info is not None:
self.emit('music-track-changed', info) self.emit('music-track-changed', info)
def _vanished(self, connection, name, *user_data): def _remove_player(self, name):
log.info('%s vanished', name) log.info('%s vanished', name)
if name in self.con: if name in self.players:
connection.signal_unsubscribe( self.connection.signal_unsubscribe(
self.con[name]) self.players[name])
self.con.pop(name) self.players.pop(name)
self.emit('music-track-changed', None) self.emit('music-track-changed', None)
@ -176,4 +204,4 @@ if __name__ == '__main__':
print(music_track_info.title) print(music_track_info.title)
listener = MusicTrackListener.get() listener = MusicTrackListener.get()
listener.connect('music-track-changed', music_track_change_cb) listener.connect('music-track-changed', music_track_change_cb)
GObject.MainLoop().run() GLib.MainLoop().run()