-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: register a D-Bus service per widget to apply presets
Should be enabled first from the widget settings General tab example usage: qdbus6 luisbocanegra.panel.colorizer.c337.w2346 /preset luisbocanegra.panel.colorizer.c337.w2346.preset /path/to/preset/dir refs: #126
- Loading branch information
1 parent
8028e59
commit 2b8f9b6
Showing
6 changed files
with
282 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import QtQuick | ||
import org.kde.plasma.plasmoid | ||
|
||
|
||
Item { | ||
|
||
id: root | ||
property bool enabled: false | ||
property string preset: "" | ||
property bool switchIsPending: false | ||
property int poolingRate: 250 | ||
|
||
property string toolsDir: Qt.resolvedUrl("./tools").toString().substring(7) + "/" | ||
property string serviceUtil: toolsDir+"service.py" | ||
property string pythonExecutable: plasmoid.configuration.pythonExecutable | ||
property string serviceCmd: pythonExecutable + " '" + serviceUtil + "' " + Plasmoid.containment.id + " " + Plasmoid.id | ||
property string dbusName: Plasmoid.metaData.pluginId + ".c" + Plasmoid.containment.id + ".w" + Plasmoid.id | ||
property string gdbusPartial: "gdbus call --session --dest "+dbusName+" --object-path /preset --method "+dbusName | ||
property string pendingSwitchCmd: gdbusPartial +".pending_switch" | ||
property string switchDoneCmd: gdbusPartial +".switch_done" | ||
property string getPresetCmd: gdbusPartial +".preset" | ||
property string quitServiceCmd: gdbusPartial +".quit" | ||
|
||
RunCommand { | ||
id: runCommand | ||
onExited: (cmd, exitCode, exitStatus, stdout, stderr) => { | ||
// console.error(cmd, exitCode, exitStatus, stdout, stderr) | ||
if (exitCode!==0) return | ||
stdout = stdout.trim().replace(/[()',]/g, "") | ||
// console.log("stdout parsed:", stdout) | ||
if(cmd === pendingSwitchCmd) { | ||
switchIsPending = stdout === "true" | ||
} | ||
if (cmd === getPresetCmd) { | ||
preset = stdout | ||
switchIsPending = false | ||
} | ||
} | ||
} | ||
|
||
Component.onCompleted: { | ||
toggleService() | ||
} | ||
|
||
function toggleService() { | ||
if (enabled) { | ||
runCommand.run(serviceCmd) | ||
} else ( | ||
runCommand.run(quitServiceCmd) | ||
) | ||
} | ||
|
||
onEnabledChanged: toggleService() | ||
|
||
onSwitchIsPendingChanged: { | ||
if (switchIsPending) { | ||
runCommand.run(switchDoneCmd) | ||
runCommand.run(getPresetCmd) | ||
} | ||
} | ||
|
||
Timer { | ||
id: updateTimer | ||
interval: poolingRate | ||
running: enabled | ||
repeat: true | ||
onTriggered: { | ||
if (switchIsPending) return | ||
runCommand.run(pendingSwitchCmd) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env python | ||
""" | ||
D-Bus service to interact with the current panel | ||
""" | ||
|
||
import sys | ||
import dbus | ||
import dbus.service | ||
from dbus.mainloop.glib import DBusGMainLoop | ||
from gi.repository import GLib | ||
|
||
DBusGMainLoop(set_as_default=True) | ||
bus = dbus.SessionBus() | ||
|
||
CONTAINMENT_ID = sys.argv[1] | ||
PANEL_ID = sys.argv[2] | ||
SERVICE_NAME = "luisbocanegra.panel.colorizer.c" + CONTAINMENT_ID + ".w" + PANEL_ID | ||
PATH = "/preset" | ||
|
||
|
||
class Service(dbus.service.Object): | ||
"""D-Bus service | ||
Args: | ||
dbus (dbus.service.Object): D-Bus object | ||
""" | ||
|
||
def __init__(self): | ||
self._loop = GLib.MainLoop() | ||
self._last_preset = "" | ||
self._pending_witch = False | ||
super().__init__() | ||
|
||
def run(self): | ||
"""run""" | ||
DBusGMainLoop(set_as_default=True) | ||
bus_name = dbus.service.BusName(SERVICE_NAME, dbus.SessionBus()) | ||
dbus.service.Object.__init__(self, bus_name, PATH) | ||
|
||
print("Service running...") | ||
self._loop.run() | ||
print("Service stopped") | ||
|
||
@dbus.service.method(SERVICE_NAME, in_signature="s", out_signature="s") | ||
def preset(self, m="") -> str: | ||
"""Set and get the last applied preset | ||
Args: | ||
m (str, optional): Preset. Defaults to "". | ||
Returns: | ||
str: "saved" or current Preset | ||
""" | ||
if m: | ||
if m != self._last_preset: | ||
print(f"last_last_preset: '{m}'") | ||
self._last_preset = m | ||
self._pending_witch = True | ||
return "saved" | ||
return self._last_preset | ||
|
||
@dbus.service.method(SERVICE_NAME, in_signature="", out_signature="b") | ||
def pending_switch(self) -> bool: | ||
"""Wether there is a pending preset switch | ||
Returns: | ||
bool: Pending | ||
""" | ||
return self._pending_witch | ||
|
||
@dbus.service.method(SERVICE_NAME, in_signature="", out_signature="") | ||
def switch_done(self): | ||
"""Void the pending switch""" | ||
self._pending_witch = False | ||
|
||
@dbus.service.method(SERVICE_NAME, in_signature="", out_signature="") | ||
def quit(self): | ||
"""Stop the service""" | ||
print("Shutting down") | ||
self._loop.quit() | ||
|
||
|
||
if __name__ == "__main__": | ||
# Keep a single instance of the service | ||
try: | ||
bus.get_object(SERVICE_NAME, PATH) | ||
print("Service is already running") | ||
except dbus.exceptions.DBusException: | ||
Service().run() |