OBSPlugin/OBSActionBase.py

120 lines
4.5 KiB
Python
Raw Normal View History

2023-11-25 13:49:41 +00:00
from src.backend.PluginManager.ActionBase import ActionBase
from src.backend.PluginManager.PluginBase import PluginBase
from src.backend.DeckManagement.DeckController import DeckController
from src.backend.PageManagement.Page import Page
from src.backend.PluginManager.PluginBase import PluginBase
2023-11-25 13:49:41 +00:00
# Import gtk modules
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk, Adw
2024-04-20 07:02:00 +00:00
import threading
from loguru import logger as log
2023-11-25 13:49:41 +00:00
class OBSActionBase(ActionBase):
2024-05-10 15:33:23 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2024-04-13 12:36:22 +00:00
self.has_configuration = True
2023-11-25 13:49:41 +00:00
self.status_label = Gtk.Label(label=self.plugin_base.lm.get("actions.base.status.no-connection"), css_classes=["bold", "red"])
2023-11-25 13:49:41 +00:00
if not self.plugin_base.backend.get_connected():
self.reconnect_obs()
2023-11-25 13:49:41 +00:00
def get_config_rows(self) -> list:
self.ip_entry = Adw.EntryRow(title=self.plugin_base.lm.get("actions.base.ip.label"))
2023-11-25 13:49:41 +00:00
self.port_spinner = Adw.SpinRow.new_with_range(0, 65535, 1)
self.port_spinner.set_title(self.plugin_base.lm.get("actions.base.port.label"))
self.password_entry = Adw.PasswordEntryRow(title=self.plugin_base.lm.get("actions.base.password.label"))
2023-11-25 13:49:41 +00:00
self.load_config_defaults()
# Connect signals
self.ip_entry.connect("notify::text", self.on_change_ip)
self.port_spinner.connect("notify::value", self.on_change_port)
self.password_entry.connect("notify::text", self.on_change_password)
return [self.ip_entry, self.port_spinner, self.password_entry]
def load_config_defaults(self):
settings = self.plugin_base.get_settings()
2023-11-25 13:49:41 +00:00
ip = settings.setdefault("ip", "localhost")
port = settings.setdefault("port", 4455)
password = settings.setdefault("password", "")
# Update ui
self.ip_entry.set_text(ip)
self.port_spinner.set_value(port)
self.password_entry.set_text(password)
self.update_ip_warning_status()
self.update_status_label()
2023-11-25 13:49:41 +00:00
def on_change_ip(self, entry, *args):
settings = self.plugin_base.get_settings()
settings["ip"] = self.ip_entry.get_text().strip()
self.plugin_base.set_settings(settings)
2023-11-25 13:49:41 +00:00
self.update_ip_warning_status()
2023-11-25 13:49:41 +00:00
self.reconnect_obs()
def update_ip_warning_status(self):
valid = self.plugin_base.backend.OBSController.validate_ip(self.ip_entry.get_text().strip())
if valid:
self.ip_entry.remove_css_class("error")
else:
self.ip_entry.add_css_class("error")
2023-11-25 13:49:41 +00:00
def on_change_port(self, spinner, *args):
settings = self.plugin_base.get_settings()
2023-11-25 13:49:41 +00:00
settings["port"] = int(spinner.get_value())
self.plugin_base.set_settings(settings)
2023-11-25 13:49:41 +00:00
self.reconnect_obs()
def on_change_password(self, entry, *args):
settings = self.plugin_base.get_settings()
2023-11-25 13:49:41 +00:00
settings["password"] = entry.get_text()
self.plugin_base.set_settings(settings)
2023-11-25 13:49:41 +00:00
self.reconnect_obs()
def reconnect_obs(self):
2024-04-20 07:02:00 +00:00
threading.Thread(target=self._reconnect_obs, daemon=True, name="reconnect_obs").start()
def _reconnect_obs(self):
try:
self.plugin_base.backend.connect_to(
2024-07-03 15:24:32 +00:00
host=self.plugin_base.get_settings().get("ip", "localhost"),
port=self.plugin_base.get_settings().get("port", 4455),
password=self.plugin_base.get_settings().get("password") or "",
2024-04-20 07:02:00 +00:00
timeout=3, legacy=False)
except Exception as e:
log.error(e)
if hasattr(self, "status_label"):
self.update_status_label()
def update_status_label(self) -> None:
2024-04-20 07:02:00 +00:00
threading.Thread(target=self._update_status_label, daemon=True, name="update_status_label").start()
def _update_status_label(self):
if self.plugin_base.backend.get_connected():
print("connected - label")
self.status_label.set_label(self.plugin_base.lm.get("actions.base.status.connected"))
2023-11-25 13:49:41 +00:00
self.status_label.remove_css_class("red")
self.status_label.add_css_class("green")
else:
print("not connected - label")
self.status_label.set_label(self.plugin_base.lm.get("actions.base.status.no-connection"))
2023-11-25 13:49:41 +00:00
self.status_label.remove_css_class("green")
self.status_label.add_css_class("red")
def get_custom_config_area(self):
self.update_status_label()
return self.status_label