diff --git a/README.md b/README.md index ba0f752..edc6bae 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Profanity LED notification -Sends new message notifications to a predefined LED by running an user-set command. Inspired by a similar [Pidgin plugin](https://sites.google.com/site/simohmattila/led-notification). LED turns off once the chat or room window receives focus. +Sends new message notifications to a predefined LED by running an user-set command. Inspired by a similar [Pidgin plugin](https://sites.google.com/site/simohmattila/led-notification). +Emulates "/tray on" "/tray read off" behaviour. + ## Install: As with other Profanity plugins, simply clone the repository and run: ``` diff --git a/lednotification.py b/lednotification.py index c037eca..9311ce4 100644 --- a/lednotification.py +++ b/lednotification.py @@ -1,66 +1,80 @@ import subprocess from time import sleep - import prof -focused = False +led_thread = None +last_window_jid = None +unfocused_jid = {} def call_command(state_on=True): if state_on: command = prof.settings_string_get("lednotification", "command_on", "xset led 3") else: command = prof.settings_string_get("lednotification", "command_off", "xset -led 3") - subprocess.call(command) + subprocess.call(command, shell=True) def enable_led(state_on=True): # Can be expanded for different ways to call LED changes call_command(state_on) def led_notify(): - global focused - focused = False - blink = prof.settings_string_get("lednotification", "blink", "off") - if blink != "off": - sleep_interval = 0.5 if blink == "on" else blink - while not focused: - enable_led(True) - sleep(sleep_interval) - enable_led(False) - else: - enable_led(True) + enable_led(True) -def prof_on_chat_win_focus(): - on_focus() +def prof_on_chat_win_focus(barejid): + on_focus(barejid) + return None -def prof_on_room_win_focus(): +def prof_on_room_win_focus(barejid): rooms = prof.settings_string_get("lednotification", "rooms", "off") if rooms == "on": - on_focus() + on_focus(barejid) + return None -def on_focus(): +def on_focus(jid): global focused + global last_window_jid + global unfocused_jid + prof.cons_show("!!!" + unfocused_jid + " ; " + bool(unfocused_jid)) focused = True - enable_led(False) + last_window_jid = jid + unfocused_jid.pop(jid, None) + if led_thread: + led_thread.join() + led_thread = None + if not unfocused_jid: + enable_led(False) def prof_post_chat_message_display(barejid, resource, message): - enabled = prof.settings_string_get("lednotification", "enabled", "on") - if enabled == "on": + prof.cons_show('test1234') + prof.cons_show("##!!!" + unfocused_jid + " ; " + bool(unfocused_jid)) + if check_chat_notify(barejid): led_notify() return message def prof_post_room_message_display(barejid, nick, message): - enabled = prof.settings_string_get("lednotification", "enabled", "on") - rooms = prof.settings_string_get("lednotification", "rooms", "off") - user = prof.get_room_nick(barejid) - if enabled == "on" and rooms == "on" and user != nick: + if check_chat_notify(barejid) and check_room_notify(barejid, nick): led_notify() return message def prof_post_priv_message_display(barejid, nick, message): - enabled = prof.settings_string_get("lednotification", "enabled", "on") - if enabled == "on": + if check_chat_notify(barejid): led_notify() return message +def check_chat_notify(barejid): + global unfocused_jid + + is_console = prof.current_win_is_console() + if last_window_jid != barejid or is_console: + unfocused_jid[barejid] = True + prof.cons_show("CCC##!!!" + unfocused_jid + " ; " + bool(unfocused_jid)) + enabled = prof.settings_string_get("lednotification", "enabled", "on") + return enabled == "on" and is_console + +def check_room_notify(barejid, nick): + rooms = prof.settings_string_get("lednotification", "rooms", "off") + user = prof.get_room_nick(barejid) + return rooms == "on" and user != nick + def _cmd_say(arg1=None, arg2=None): if arg1 == "on": prof.settings_string_set("lednotification", "enabled", "on")