mpris2_support.py 2.54 KB
Newer Older
Dicson's avatar
Dicson committed
1 2
# -*- coding: utf-8 -*-

3 4
import os

Dicson's avatar
Dicson committed
5 6 7 8
from plugins import GajimPlugin
from plugins.helpers import log_calls
from common import dbus_support

9 10
ERR_MSG = ''

Dicson's avatar
Dicson committed
11
if dbus_support.supported:
12
    from music_track_listener import MusicTrackListener
13 14 15 16 17
else:
    ERR_MSG = 'D-Bus Python bindings are missing'

if os.name == 'nt':
    ERR_MSG = 'Plugin can\'t be run under Windows.'
18 19 20 21 22 23


class MusicTrackInfo(object):
    __slots__ = ['title', 'album', 'artist', 'duration', 'track_number',
        'paused', 'url', 'albumartist']

Dicson's avatar
Dicson committed
24 25 26 27

class Mpris2Plugin(GajimPlugin):
    @log_calls('Mpris2Plugin')
    def init(self):
28 29
        self.description = _('MPRIS2 support. Allows to update status message '
        'according to the music you\'re listening via the MPRIS2 D-Bus API.')
Dicson's avatar
Dicson committed
30
        self.config_dialog = None
31 32 33 34
        if ERR_MSG:
            self.available_text = ERR_MSG
            self.activatable = False
            return
Dicson's avatar
Dicson committed
35 36 37 38 39
        self.artist = self.title = self.source = ''
        self.listener = MusicTrackListener().get()

    @log_calls('NowListenPlugin')
    def activate(self):
40
        self._last_playing_music = None
Dicson's avatar
Dicson committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        self.bus = dbus_support.session_bus.SessionBus()
        self.bus.add_signal_receiver(self.properties_changed,
            "PropertiesChanged", "org.freedesktop.DBus.Properties")

    @log_calls('Mpris2Plugin')
    def deactivate(self):
        self.bus.remove_signal_receiver(self.properties_changed,
            "PropertiesChanged", "org.freedesktop.DBus.Properties")

    def properties_changed(self,*args,**kw):
        if args[0] != 'org.mpris.MediaPlayer2.Player':
            return
        if 'PlaybackStatus' in args[1]:
            if args[1]['PlaybackStatus'] in ['Paused', 'Stopped']:
                self.title = self.artist = self.source = ''
                self.listener.emit('music-track-changed', None)
57 58 59
            if args[1]['PlaybackStatus'] == 'Playing':
                self.listener.emit('music-track-changed',
                    self._last_playing_music)
Dicson's avatar
Dicson committed
60 61 62 63 64 65 66 67
        if 'Metadata' not in args[1]:
            return

        data = args[1]['Metadata']
        info = MusicTrackInfo()
        info.title = data.get("xesam:title", '')
        info.album = data.get("xesam:album", '')
        info.artist = data.get("xesam:artist", [''])[0]
68
        info.albumartist = data.get("xesam:albumArtist", [''])[0]
Dicson's avatar
Dicson committed
69 70
        info.duration = int(data.get('mpris:length', 0))
        info.track_number = int(data.get('xesam:trackNumber', 0))
71 72
        info.url = data.get("xesam:url", '')

73
        self._last_playing_music = info
Dicson's avatar
Dicson committed
74
        self.listener.emit('music-track-changed', info)