Try to fix repeating welcome message.

This commit is contained in:
Storm Dragon 2025-04-04 14:32:03 -04:00
parent 7876a18c12
commit 231d74efa0

View File

@ -21,10 +21,15 @@
"""Hello Cthulhu plugin for Cthulhu."""
import logging
import weakref
from cthulhu.plugin import Plugin, cthulhu_hookimpl
logger = logging.getLogger(__name__)
# Class-level variable to track if the greeting has been presented
# This ensures the greeting is only shown once even if multiple instances exist
_greeting_shown = False
class HelloCthulhu(Plugin):
"""Plugin that speaks a welcome message when Cthulhu starts up."""
@ -33,6 +38,7 @@ class HelloCthulhu(Plugin):
super().__init__(*args, **kwargs)
logger.info("HelloCthulhu plugin initialized")
self._signal_handler_id = None
self._is_connected = False
@cthulhu_hookimpl
def activate(self, plugin=None):
@ -43,13 +49,16 @@ class HelloCthulhu(Plugin):
logger.info("Activating HelloCthulhu plugin")
try:
# Connect to the start-application-completed signal
# Only connect the signal if we haven't already
if not self._is_connected:
signal_manager = self.app.getSignalManager()
self._signal_handler_id = signal_manager.connectSignal(
"start-application-completed",
self.process,
"default" # Add profile parameter
)
self._is_connected = True
logger.debug("Connected to start-application-completed signal")
except Exception as e:
logger.error(f"Error activating HelloCthulhu plugin: {e}")
@ -62,23 +71,43 @@ class HelloCthulhu(Plugin):
logger.info("Deactivating HelloCthulhu plugin")
try:
# Disconnect signal if we have an ID
if self._signal_handler_id is not None:
# Only disconnect if we're connected
if self._is_connected and self._signal_handler_id is not None:
signal_manager = self.app.getSignalManager()
# Use disconnectSignalByFunction instead since disconnectSignal doesn't exist
signal_manager.disconnectSignalByFunction(
self.process
)
self._signal_handler_id = None
self._is_connected = False
logger.debug("Disconnected from start-application-completed signal")
except Exception as e:
logger.error(f"Error deactivating HelloCthulhu plugin: {e}")
def process(self, app):
"""Process the start-application-completed signal."""
global _greeting_shown
# Only present the message if it hasn't been shown yet
if _greeting_shown:
logger.debug("Greeting already shown, skipping")
return
try:
messages = app.getDynamicApiManager().getAPI('Messages')
state = app.getDynamicApiManager().getAPI('CthulhuState')
if state.activeScript:
state.activeScript.presentMessage(messages.START_CTHULHU, resetStyles=False)
_greeting_shown = True
logger.info("Greeting message presented")
# Disconnect the signal after presenting the message
if self._is_connected and self._signal_handler_id is not None:
signal_manager = app.getSignalManager()
signal_manager.disconnectSignalByFunction(
self.process
)
self._signal_handler_id = None
self._is_connected = False
logger.debug("Disconnected signal after presenting greeting")
except Exception as e:
logger.error(f"Error in HelloCthulhu process: {e}")