Last active
October 31, 2019 05:20
-
-
Save raphant/991d33b11877ab177dbf6ab8e72d10b3 to your computer and use it in GitHub Desktop.
[KDE ONLY][Python3+] Automatically change your default browser when changing activities on KDE.
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
#!/path/to/python3 | |
"""" | |
If running as an executable: | |
Don't forget to chmod +x this file when adding it as a service. | |
And Also don't forget to modify the /path/to/python above | |
Requirements: | |
dbus-python: https://dbus.freedesktop.org/doc/dbus-python/index.html (pip install dbus-python) | |
gi-repository: https://pygobject.readthedocs.io/en/latest/getting_started.html (follow instructions on website) | |
sh: https://amoffat.github.io/sh/ (pip install sh) | |
""" | |
import getpass | |
import dbus | |
from dbus import SessionBus | |
from dbus.mainloop.glib import DBusGMainLoop | |
import logging | |
from sh import kreadconfig5, kwriteconfig5, qdbus, RunningCommand | |
# make sure we run as the user and not system or anything else | |
# running as '~/.config/kdeglobals' does not work on my system | |
kde_globals = f'/home/{getpass.getuser()}/.config/kdeglobals' | |
service = 'org.kde.ActivityManager' | |
interface = service + '.Activities' | |
path = '/ActivityManager/Activities' | |
signal = 'CurrentActivityChanged' | |
logging.basicConfig() | |
logger = logging.getLogger('monitor-activities') | |
logger.setLevel(logging.INFO) | |
def read_conf() -> str: | |
# kreadconfig5 --file ~/.config/kdeglobals --group General --key BrowserApplication | |
cur_browser = kreadconfig5(file=kde_globals, | |
group='General', key='BrowserApplication') # type: RunningCommand | |
return str(cur_browser).strip() | |
def write_conf(text) -> None: | |
# kwriteconfig5 --file ~/.config/kdeglobals --group General --key BrowserApplication "$1" | |
kwriteconfig5(f'--file={kde_globals}', '--group=General', '--key=BrowserApplication', | |
text) # type: RunningCommand | |
def filter_cb(_: SessionBus, message: dbus.lowlevel.SignalMessage) -> None: | |
# [ "$member" == "$signal" ] || continue | |
if message.get_member() != "CurrentActivityChanged": | |
return | |
try: | |
# curact=$(qdbus $service $path $interface.CurrentActivity) | |
current_activity = str(qdbus(service, path, interface + '.CurrentActivity')).strip() | |
# name="$(qdbus $service $path $interface.ActivityName $curact)" | |
name = str(qdbus(service, path, interface + '.ActivityName', current_activity)).strip() | |
# echo "Previous browser is $(readconf)" | |
logger.info('Previous browser was %s', read_conf()) | |
# These suited me perfectly. Change it to your liking :) | |
if name == 'Default': | |
write_conf('brave.desktop') | |
elif name == 'Work': | |
write_conf('chromium.desktop') | |
elif name == 'Research': | |
write_conf('opera.desktop') | |
else: | |
write_conf('firefox.desktop') | |
# echo "Switched to activity $name" | |
logger.info('Switched to %s', name) | |
# echo "Current Browser is $(readconf)" | |
logger.info('Current browser is %s', read_conf()) | |
except Exception as e: | |
logger.exception(e) | |
def monitor() -> None: | |
# dbus-monitor --profile "type=signal,path=$path,interface=$interface,member=$signal" | |
DBusGMainLoop(set_as_default=True) | |
bus = dbus.SessionBus() | |
bus.add_match_string(f'type=signal,path={path},interface={interface},member={signal}') | |
bus.add_message_filter(filter_cb) | |
# Monitoring process | |
from gi.repository import GLib | |
loop = GLib.MainLoop() | |
loop.run() | |
monitor() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment