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.""" """Hello Cthulhu plugin for Cthulhu."""
import logging import logging
import weakref
from cthulhu.plugin import Plugin, cthulhu_hookimpl from cthulhu.plugin import Plugin, cthulhu_hookimpl
logger = logging.getLogger(__name__) 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): class HelloCthulhu(Plugin):
"""Plugin that speaks a welcome message when Cthulhu starts up.""" """Plugin that speaks a welcome message when Cthulhu starts up."""
@ -33,6 +38,7 @@ class HelloCthulhu(Plugin):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
logger.info("HelloCthulhu plugin initialized") logger.info("HelloCthulhu plugin initialized")
self._signal_handler_id = None self._signal_handler_id = None
self._is_connected = False
@cthulhu_hookimpl @cthulhu_hookimpl
def activate(self, plugin=None): def activate(self, plugin=None):
@ -43,13 +49,16 @@ class HelloCthulhu(Plugin):
logger.info("Activating HelloCthulhu plugin") logger.info("Activating HelloCthulhu plugin")
try: try:
# Connect to the start-application-completed signal # Only connect the signal if we haven't already
signal_manager = self.app.getSignalManager() if not self._is_connected:
self._signal_handler_id = signal_manager.connectSignal( signal_manager = self.app.getSignalManager()
"start-application-completed", self._signal_handler_id = signal_manager.connectSignal(
self.process, "start-application-completed",
"default" # Add profile parameter self.process,
) "default" # Add profile parameter
)
self._is_connected = True
logger.debug("Connected to start-application-completed signal")
except Exception as e: except Exception as e:
logger.error(f"Error activating HelloCthulhu plugin: {e}") logger.error(f"Error activating HelloCthulhu plugin: {e}")
@ -62,23 +71,43 @@ class HelloCthulhu(Plugin):
logger.info("Deactivating HelloCthulhu plugin") logger.info("Deactivating HelloCthulhu plugin")
try: try:
# Disconnect signal if we have an ID # Only disconnect if we're connected
if self._signal_handler_id is not None: if self._is_connected and self._signal_handler_id is not None:
signal_manager = self.app.getSignalManager() signal_manager = self.app.getSignalManager()
# Use disconnectSignalByFunction instead since disconnectSignal doesn't exist
signal_manager.disconnectSignalByFunction( signal_manager.disconnectSignalByFunction(
self.process self.process
) )
self._signal_handler_id = None self._signal_handler_id = None
self._is_connected = False
logger.debug("Disconnected from start-application-completed signal")
except Exception as e: except Exception as e:
logger.error(f"Error deactivating HelloCthulhu plugin: {e}") logger.error(f"Error deactivating HelloCthulhu plugin: {e}")
def process(self, app): def process(self, app):
"""Process the start-application-completed signal.""" """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: try:
messages = app.getDynamicApiManager().getAPI('Messages') messages = app.getDynamicApiManager().getAPI('Messages')
state = app.getDynamicApiManager().getAPI('CthulhuState') state = app.getDynamicApiManager().getAPI('CthulhuState')
if state.activeScript: if state.activeScript:
state.activeScript.presentMessage(messages.START_CTHULHU, resetStyles=False) 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: except Exception as e:
logger.error(f"Error in HelloCthulhu process: {e}") logger.error(f"Error in HelloCthulhu process: {e}")