Created
January 22, 2025 22:07
-
-
Save jpsutton/ba8e62f6673b6a919e4bfb6da1acb784 to your computer and use it in GitHub Desktop.
DBus listener which looks for Evolution new mail and calendar reminder notifications and plays audio in repsonse
This file contains hidden or 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
#!/usr/bin/env python3 | |
import os | |
import sys | |
import dbus | |
import pygame | |
import argparse | |
import datetime | |
from gi.repository import GLib | |
from dbus.mainloop.glib import DBusGMainLoop | |
class NotificationMonitor: | |
def __init__(self, newmail_file, reminder_file): | |
# Setup audio players | |
pygame.mixer.init() | |
self.newmail_sound = pygame.mixer.Sound(newmail_file) | |
self.reminder_sound = pygame.mixer.Sound(reminder_file) | |
# Connect to DBus Session Bus | |
DBusGMainLoop(set_as_default=True) | |
self.bus = dbus.SessionBus() | |
# Process all DBus signals to look for calendar reminders | |
self.bus.add_signal_receiver(self.reminder_handler) | |
# Process signals from Evolutions mail component to look for new mail notifications | |
self.bus.add_match_string_non_blocking("interface='org.gnome.evolution.mail.dbus.Signal'") | |
self.bus.add_message_filter(self.newmail_handler) | |
def reminder_handler(self, *args): | |
try: | |
source, data, _ = args | |
if source == "org.gnome.evolution.dataserver.Source" and 'Data' in data and '[Calendar]' in data['Data']: | |
print(f"{datetime.datetime.now().isoformat()}::{source}::Calendar Reminder") | |
self.reminder_sound.play() | |
except ValueError: | |
pass | |
def newmail_handler(self, bus, message): | |
if message.get_member() is not None and message.get_member().strip().lower() == "newmail": | |
print(f"{datetime.datetime.now().isoformat()}::{message.get_interface()}::{message.get_member()}") | |
self.newmail_sound.play() | |
def run(self): | |
loop = GLib.MainLoop() | |
try: | |
loop.run() | |
except KeyboardInterrupt: | |
pygame.mixer.quit() | |
sys.exit(0) | |
def main(): | |
parser = argparse.ArgumentParser(description='Monitor DBus notifications and play sounds') | |
parser.add_argument('--newmail', required=True, help='Path to sound file to play when new mail arrives') | |
parser.add_argument('--reminder', required=True, help='Path to sound file to play when calendar reminders are sent') | |
args = parser.parse_args() | |
if not os.path.exists(args.newmail): | |
print(f"Error: Sound file {args.newmail} not found") | |
sys.exit(1) | |
if not os.path.exists(args.reminder): | |
print(f"Error: Sound file {args.reminder} not found") | |
sys.exit(2) | |
monitor = NotificationMonitor(args.newmail, args.reminder) | |
monitor.run() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This application is probably not needed if you run Gnome, but I couldn't get Evolution's audio notifications working on Plasma Desktop, hence this gist. The following systemd user service is useful to run this while logged in: