Skip to content

Commit

Permalink
switch from dbus-python to gio
Browse files Browse the repository at this point in the history
  • Loading branch information
deltragon committed Jul 5, 2024
1 parent d834af9 commit 894bbaa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -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.
.
Expand Down
2 changes: 1 addition & 1 deletion safeeyes/plugins/mediacontrol/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.0.1"
},
"dependencies": {
"python_modules": ["dbus"],
"python_modules": [],
"shell_commands": [],
"operating_systems": [],
"desktop_environments": [],
Expand Down
34 changes: 25 additions & 9 deletions safeeyes/plugins/mediacontrol/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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):
Expand Down
24 changes: 18 additions & 6 deletions safeeyes/safeeyes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 894bbaa

Please sign in to comment.