#!./venv/bin/python3 """pywmreceived.py WindowMaker dockapp pidgin messages Copyright (C) 2006 Mario Frasca Licensed under the GNU General Public License. """ import logging from wmdocklib import wmoo as wmoo import dbus import dbus.mainloop.glib from gi.repository import GLib import threading from icecream import ic from xpm_resources import palette, background, patterns line_height = 9 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) class Application(wmoo.Application): def __init__(self, *args, **kwargs): wmoo.Application.__init__(self, *args, **kwargs) self._count = 0 self._flasher = 0 self.backlit = 0 self.lines = [ # name, messages received [" CATHY", 0], [" FRANK", 0], [" TIM", 0], [" LEE", 0], [" TANDA", 0], [" OTHER", 0], ] # Initialize D-Bus and connect to Pidgin's ReceivedIMMsg signal self.register_dbus() def register_dbus(self): try: # Set up the D-Bus main loop dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) # Connect to the session bus bus = dbus.SessionBus() # Obtain the Pidgin D-Bus service object purple_service = bus.get_object( "im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject" ) # Get the interface to interact with purple_interface = dbus.Interface( purple_service, "im.pidgin.purple.PurpleInterface" ) # Connect the ReceivedIMMsg signal to the handler purple_interface.connect_to_signal( "ReceivedImMsg", self.handle_received_im_msg ) # Connect the SentIMMsg signal to the handler purple_interface.connect_to_signal( "SentImMsg", self.handle_sent_im_msg ) ic("Connected to Pidgin's ReceivedIMMsg signal successfully.") except dbus.DBusException as e: print("Failed to connect to Pidgin's D-Bus interface:", e) def handle_received_im_msg(self, account, sender, message, conversation, flags): """ Callback function that handles the ReceivedIMMsg signal. Prints the sender and message. Parameters: account (str): The account from which the message was received. sender (str): The sender's identifier. message (str): The message content. conversation (str): The conversation identifier. flags (int): Message flags. """ ic("") ic(f"sender: {sender}") ic(f"message: {message}") self._flasher = 7 def handle_sent_im_msg(self, account, recepient, message): """ Callback function that handles the SentImMsg signal. Prints the sender and message. Parameters: recepient (str): The recepien's identifier. message (str): The message content. """ ic("") ic(f"recepient: {recepient}") ic(f"message: {message}") def draw_string(self, xstart, ystart, text): for char in text: if char >= "A" and char <="Z": x = (ord(char) -65) * 6 y = 1 elif char >= "0" and char <="9": x = (ord(char) -48) * 6 y = 10 elif char == " ": x = 6 * 10 y = 10 elif char == "-": x = 6 * 11 y = 10 elif char == ".": x = 6 * 12 y = 10 elif char == "'": x = 6 * 13 y = 10 elif char == "(": x = 6 * 14 y = 10 elif char == ")": x = 6 * 15 y = 10 elif char == "*": x = 6 * 16 y = 10 elif char == "/": x = 6 * 17 y = 10 else: continue self.putPattern(x, y + (self.backlit * 17), 6, 7, xstart, ystart) xstart += 6 def draw_background(self): self.putPattern(0 + (self.backlit * 62), 36, 64, 64, 0, 0) def draw_all_text(self): for index, line in enumerate(self.lines[:6]): self.draw_string(9, 6 + (index * line_height), line[0]) def toggle_backlight(self, event): self.backlit = 1 - self.backlit self.draw_background() self.draw_all_text() def update(self): wmoo.Application.update(self) self._count += 1 if self._count <= 3: return self._count = 0 if self._flasher: self._flasher -= 1 self.toggle_backlight(True) def run_glib_mainloop(): """ Runs the GLib main loop. This should be executed in a separate thread. """ loop = GLib.MainLoop() ic("Start Loop") try: loop.run() except KeyboardInterrupt: loop.quit() def main(): """ The main entry point of the application. """ app = Application(font_name='5x8', margin = 3, bg=0, fg=2, palette = palette, background = background, patterns = patterns) # app.addCallback(app.previousRadio, 'buttonrelease', area=( 6,29,15,38)) # 6x7 grey1=1 grey2=10 green1=18 green2=27 app.draw_background() app.draw_all_text() app.addCallback(app.toggle_backlight, 'buttonrelease', area=(2,2,62,62)) # Start the GLib main loop in a separate thread glib_thread = threading.Thread(target=run_glib_mainloop, daemon=True) glib_thread.start() # Run the application's main loop app.run() if __name__ == '__main__': main()