Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion aw_qt/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import List, Any
import logging
import os
from typing import Any, List, Optional

import tomlkit
from aw_core import dirs
from aw_core.config import load_config_toml

logger = logging.getLogger(__name__)

default_config = """
[aw-qt]
Expand All @@ -12,6 +17,60 @@
""".strip()


def _read_server_rust_port(testing: bool) -> Optional[int]:
"""Read port from aw-server-rust config, returns None if not found/set."""
config_dir = dirs.get_config_dir("aw-server-rust")
config_file = "config-testing.toml" if testing else "config.toml"
config_path = os.path.join(config_dir, config_file)

if not os.path.isfile(config_path):
return None

try:
with open(config_path) as f:
config = tomlkit.parse(f.read())
if "port" in config:
return int(str(config["port"]))
except Exception as e:
logger.warning("Failed to read aw-server-rust config: %s", e)
return None


def _read_aw_server_port(testing: bool) -> Optional[int]:
"""Read port from aw-server (Python) config, returns None if not found/set."""
config_dir = dirs.get_config_dir("aw-server")
config_path = os.path.join(config_dir, "aw-server.toml")
section = "server-testing" if testing else "server"

if not os.path.isfile(config_path):
return None

try:
with open(config_path) as f:
config = tomlkit.parse(f.read())
section_data = config.get(section, {})
if "port" in section_data:
return int(str(section_data["port"]))
except Exception as e:
logger.warning("Failed to read aw-server config: %s", e)
return None


def _read_server_port(testing: bool) -> int:
"""Read port from server config (aw-server-rust or aw-server), falling back to defaults."""
default_port = 5666 if testing else 5600

port = _read_server_rust_port(testing)
if port is not None:
return port

port = _read_aw_server_port(testing)
if port is not None:
return port

return default_port


class AwQtSettings:
def __init__(self, testing: bool):
"""
Expand All @@ -22,3 +81,4 @@ def __init__(self, testing: bool):
config_section: Any = config["aw-qt" if not testing else "aw-qt-testing"]

self.autostart_modules: List[str] = config_section["autostart_modules"]
self.port: int = _read_server_port(testing)
2 changes: 1 addition & 1 deletion aw_qt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def main(
from . import trayicon # pylint: disable=import-outside-toplevel

# run the trayicon, wait for signal to quit
error_code = trayicon.run(manager, testing=testing)
error_code = trayicon.run(manager, testing=testing, port=config.port)
elif interactive_cli:
# just an experiment, don't really see the use right now
_interactive_cli(manager)
Expand Down
9 changes: 6 additions & 3 deletions aw_qt/trayicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(
icon: QIcon,
parent: Optional[QWidget] = None,
testing: bool = False,
port: Optional[int] = None,
) -> None:
QSystemTrayIcon.__init__(self, icon, parent)
self._parent = parent # QSystemTrayIcon also tries to save parent info but it screws up the type info
Expand All @@ -91,7 +92,9 @@ def __init__(
self.testing = testing
self._restart_timestamps: Dict[str, List[float]] = {}

self.root_url = f"http://localhost:{5666 if self.testing else 5600}"
if port is None:
port = 5666 if testing else 5600
self.root_url = f"http://localhost:{port}"
self.activated.connect(self.on_activated)

self._build_rootmenu()
Expand Down Expand Up @@ -263,7 +266,7 @@ def exit(manager: Manager) -> None:
QApplication.quit()


def run(manager: Manager, testing: bool = False) -> Any:
def run(manager: Manager, testing: bool = False, port: Optional[int] = None) -> Any:
logger.info("Creating trayicon...")
# print(QIcon.themeSearchPaths())

Expand Down Expand Up @@ -330,7 +333,7 @@ def run(manager: Manager, testing: bool = False) -> Any:
else:
icon = QIcon("icons:logo.png")

trayIcon = TrayIcon(manager, icon, widget, testing=testing)
trayIcon = TrayIcon(manager, icon, widget, testing=testing, port=port)
trayIcon.show()

# Re-apply tooltip after show() to ensure it registers with the
Expand Down