From 894bbaa5ef4fac2ed8794e7b422e4a36fa4f5fc2 Mon Sep 17 00:00:00 2001 From: deltragon Date: Sun, 31 Dec 2023 00:01:57 +0100 Subject: [PATCH] switch from dbus-python to gio --- debian/control | 2 +- safeeyes/plugins/mediacontrol/config.json | 2 +- safeeyes/plugins/mediacontrol/plugin.py | 34 +++++++++++++++++------ safeeyes/safeeyes.py | 24 ++++++++++++---- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/debian/control b/debian/control index 246f29ed..2124ce87 100644 --- a/debian/control +++ b/debian/control @@ -9,7 +9,7 @@ Homepage: https://github.com/slgobinath/SafeEyes/ Package: safeeyes Architecture: all -Depends: ${misc:Depends}, ${python3:Depends}, gir1.2-ayatanaappindicator3-0.1, python3 (>= 3.12.0), python3-xlib, python3-dbus, gir1.2-notify-0.7, python3-babel, x11-utils, xprintidle, alsa-utils, python3-psutil, python3-croniter +Depends: ${misc:Depends}, ${python3:Depends}, gir1.2-ayatanaappindicator3-0.1, python3 (>= 3.12.0), python3-xlib, gir1.2-notify-0.7, python3-babel, x11-utils, xprintidle, alsa-utils, python3-psutil, python3-croniter Description: Safe Eyes Safe Eyes is a simple tool to remind you to take periodic breaks for your eyes. This is essential for anyone spending more time on the computer to avoid eye strain and other physical problems. . diff --git a/safeeyes/plugins/mediacontrol/config.json b/safeeyes/plugins/mediacontrol/config.json index b4bdcef3..5c5d3326 100644 --- a/safeeyes/plugins/mediacontrol/config.json +++ b/safeeyes/plugins/mediacontrol/config.json @@ -5,7 +5,7 @@ "version": "0.0.1" }, "dependencies": { - "python_modules": ["dbus"], + "python_modules": [], "shell_commands": [], "operating_systems": [], "desktop_environments": [], diff --git a/safeeyes/plugins/mediacontrol/plugin.py b/safeeyes/plugins/mediacontrol/plugin.py index df471cd3..5c43ac33 100644 --- a/safeeyes/plugins/mediacontrol/plugin.py +++ b/safeeyes/plugins/mediacontrol/plugin.py @@ -22,12 +22,11 @@ import logging import os -import dbus import re import gi from safeeyes.model import TrayAction gi.require_version('Gtk', '3.0') -from gi.repository import Gtk +from gi.repository import Gtk, Gio tray_icon_path = None @@ -37,13 +36,31 @@ def __active_players(): List of all media players which are playing now. """ players = [] - bus = dbus.SessionBus() - for service in bus.list_names(): + dbus_proxy = Gio.DBusProxy.new_for_bus_sync( + bus_type=Gio.BusType.SESSION, + flags=Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES, + info=None, + name='org.freedesktop.DBus', + object_path='/org/freedesktop/DBus', + interface_name='org.freedesktop.DBus', + cancellable=None, + ) + + for service in dbus_proxy.ListNames(): if re.match('org.mpris.MediaPlayer2.', service): - player = bus.get_object(service, "/org/mpris/MediaPlayer2") - interface = dbus.Interface(player, 'org.freedesktop.DBus.Properties') - status = str(interface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')).lower() + player = Gio.DBusProxy.new_for_bus_sync( + bus_type=Gio.BusType.SESSION, + flags=Gio.DBusProxyFlags.NONE, + info=None, + name=service, + object_path='/org/mpris/MediaPlayer2', + interface_name='org.mpris.MediaPlayer2.Player', + cancellable=None, + ) + + status = player.get_cached_property('PlaybackStatus').unpack().lower() + if status == "playing": players.append(player) return players @@ -54,8 +71,7 @@ def __pause_players(players): Pause all playing media players using dbus. """ for player in players: - interface = dbus.Interface(player, dbus_interface='org.mpris.MediaPlayer2.Player') - interface.Pause() + player.Pause() def init(ctx, safeeyes_config, plugin_config): diff --git a/safeeyes/safeeyes.py b/safeeyes/safeeyes.py index 144181b8..d66227bc 100644 --- a/safeeyes/safeeyes.py +++ b/safeeyes/safeeyes.py @@ -25,9 +25,7 @@ import os from threading import Timer -import dbus import gi -from dbus.mainloop.glib import DBusGMainLoop from safeeyes import utility from safeeyes.ui.about_dialog import AboutDialog from safeeyes.ui.break_screen import BreakScreen @@ -189,14 +187,28 @@ def handle_suspend_callback(self, sleeping): self.plugins_manager.start() self.safe_eyes_core.start() + def handle_suspend_signal(self, proxy, sender, signal, parameters): + if signal != "PrepareForSleep": + return + + (sleeping, ) = parameters + + self.handle_suspend_callback(sleeping) + def handle_system_suspend(self): """ Setup system suspend listener. """ - DBusGMainLoop(set_as_default=True) - bus = dbus.SystemBus() - bus.add_signal_receiver(self.handle_suspend_callback, 'PrepareForSleep', - 'org.freedesktop.login1.Manager', 'org.freedesktop.login1') + self.suspend_proxy = Gio.DBusProxy.new_for_bus_sync( + bus_type=Gio.BusType.SYSTEM, + flags=Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES, + info=None, + name='org.freedesktop.login1', + object_path='/org/freedesktop/login1', + interface_name='org.freedesktop.login1.Manager', + cancellable=None, + ) + self.suspend_proxy.connect('g-signal', self.handle_suspend_signal) def on_skipped(self): """