emmental/emmental/mpris2/__init__.py

47 lines
1.4 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Implement the MPRIS2 Specification."""
from gi.repository import GObject
from gi.repository import Gio
from . import application
MPRIS2_ID = f"org.mpris.MediaPlayer2.emmental{'-debug' if __debug__ else ''}"
class Connection(GObject.GObject):
"""Our Mpris2 Object."""
dbus = GObject.Property(type=Gio.DBusConnection)
def __init__(self):
"""Initialize Mpris2."""
super().__init__()
self.app = application.Application()
self.bind_property("dbus", self.app, "dbus")
self._busid = Gio.bus_own_name(Gio.BusType.SESSION, MPRIS2_ID,
Gio.BusNameOwnerFlags.NONE,
self.__on_bus_acquired, None,
self.__on_name_lost)
def __del__(self):
"""Clean up."""
self.disconnect()
def __on_bus_acquired(self, dbus: Gio.DBusConnection, name: str) -> None:
self.dbus = dbus
self.app.register(dbus)
def __on_name_lost(self, dbus: Gio.DBusConnection, name: str) -> None:
self.app.unregister(dbus)
def disconnect(self):
"""Disconnect from dbus."""
if self.dbus:
self.app.unregister(self.dbus)
self.dbus = None
if self._busid:
Gio.bus_unown_name(self._busid)
self._busid = None