54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""Implement the MPRIS2 Specification."""
|
|
from gi.repository import GObject
|
|
from gi.repository import Gio
|
|
from .. import gsetup
|
|
from . import application
|
|
from . import player
|
|
|
|
MPRIS2_ID = f"org.mpris.MediaPlayer2.emmental{gsetup.DEBUG_STR}"
|
|
|
|
|
|
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.player = player.Player()
|
|
|
|
self.bind_property("dbus", self.app, "dbus")
|
|
self.bind_property("dbus", self.player, "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)
|
|
self.player.register(dbus)
|
|
|
|
def __on_name_lost(self, dbus: Gio.DBusConnection, name: str) -> None:
|
|
self.app.unregister(dbus)
|
|
self.player.unregister(dbus)
|
|
|
|
def disconnect(self):
|
|
"""Disconnect from dbus."""
|
|
if self.dbus:
|
|
self.app.unregister(self.dbus)
|
|
self.player.unregister(self.dbus)
|
|
self.dbus = None
|
|
|
|
if self._busid:
|
|
Gio.bus_unown_name(self._busid)
|
|
self._busid = None
|